if windows: LOG_FILE = "sort.log" logformat = logging.Formatter("%(levelname)s %(asctime)s (%(name)s) %(message)s") ##DEBUG (debug stuff & extra stuff from urllib3.connectionpool) ##INFO (regular sort operations) ##WARNING (can't find show, episode, movie...) ##ERROR (just errors) ##CRITICAL (problems with an api or filename confusion) # stdout handler # ch = logging.StreamHandler(sys.stdout) # ch.setFormatter(logformat) # log.addHandler(ch) # ^^^ adding the stdout handler here duplicates log output # file handler fh = handlers.RotatingFileHandler(LOG_FILE, maxBytes=(1048576*5), backupCount=7) fh.setFormatter(logformat) log.addHandler(fh) # read config file try: with open('/home/pi/piscripts/sort.config.json') as json_data_file: data = json.load(json_data_file) except FileNotFoundError as e: print("Config file not found, try windows path") try: with open('sort.config.json') as json_data_file: data = json.load(json_data_file) except FileNotFoundError as e: msg = "Sort config file not found, aborting" log.error(msg)
import sys import logging from datetime import date from logging.handlers import RotatingFileHandler from logging import handlers from src.read_settings import * log_file = LOG_FILE_PATH + "logs-" + str(date.today()) + ".log" logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') log = logging.getLogger('') log.setLevel(logging.INFO) format = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") ch = logging.StreamHandler(sys.stdout) ch.setFormatter(format) log.addHandler(ch) fh = handlers.RotatingFileHandler(log_file, maxBytes=(1048576 * 5), backupCount=7) fh.setFormatter(format) log.addHandler(fh)
def main(): fileOut = "TestResults.xml" server_host = "sadcsim5mon1" server_port = 8000 uri = "/RPC2" parser = OptionParser() parser.add_option( "-u", "--url", dest="c_ip", default=server_host, help="colector url default : {}".format(server_host)) parser.add_option("-p", "--port ", type="int", dest="c_port", default=server_port, help="colector port default : {}".format(server_port)) parser.add_option("-f", "--fileOut ", dest="fileOut", default=fileOut, help="result File default : '{}'".format(fileOut)) (options, args) = parser.parse_args() # print ("options : ip : {}, port : {}".format(options.c_ip,options.c_port)) server_host = options.c_ip server_port = options.c_port fileOut = options.fileOut loglevel = logging.DEBUG consloglevel = logging.DEBUG logformat = '%(asctime)s - %(funcName)s - %(levelname)s - %(message)s' logging.basicConfig(format=logformat, level=consloglevel, stream=sys.stdout) log.debug(("logfilename:{}".format(logfilename))) filelog = handlers.RotatingFileHandler(logfilename, maxBytes=1048576, backupCount=2) filelog.setLevel(loglevel) filelogformat = logging.Formatter('%(asctime)s : %(message)s') filelog.setFormatter(filelogformat) logging.getLogger("").addHandler(filelog) ################ log.info("{} START LOG SERVER {}".format("+" * 23, "+" * 24)) log.info("Log Server started http://{}:{}{}".format( server_host, str(server_port), uri)) log.info(" - In your grinder properties file be sure to have :") log.info(" grindertool.custom.logger.activate=True") log.info(" grindertool.custom.logger.url=http://{}:{}{}".format( server_host, str(server_port), uri)) log.info(" Run your campaign") log.info( " The file is generated at the end of the Campaign to {}".format( fileOut)) log.info(" The log file is generated in {}".format(logfilename)) log.debug(("starting server")) mlogServer = LogServer(server_host, server_port, uri, fileOut) mlogServer.start()
def run_simulation(sumocfg, minutes=10, tripinfo="trip_info.xml", rawgraph="raw_graph.dat", last_read_time=-1): formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_logger = logging.getLogger('Logger') file_logger.setLevel(logging.DEBUG) file_handler = handlers.RotatingFileHandler('run_sumo.log', maxBytes=200 * 1024 * 1024, backupCount=1) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(formatter) file_logger.addHandler(file_handler) seconds = 60 # milisseconds = 1000 minutes = minutes interval = minutes * seconds # * milisseconds traci.start(["sumo", "-c", sumocfg, "--tripinfo-output", tripinfo]) n_graphs = 0 graph_size = 0 id_vehicle = 1 vehicles = {} with open(rawgraph, "a") as f: while traci.simulation.getMinExpectedNumber() > 0: current_time = traci.simulation.getTime() traci.simulationStep() if current_time % interval == 0: if current_time <= last_read_time: file_logger.info("Skip time %s." % (current_time)) else: n_vehicles = 0 for veh_id in traci.vehicle.getIDList(): graph_size += 1 speed = traci.vehicle.getSpeed(veh_id) x, y = traci.vehicle.getPosition(veh_id) lon, lat = traci.simulation.convertGeo(x, y) x2, y2 = traci.simulation.convertGeo(lon, lat, fromGeo=True) if veh_id not in vehicles: index = id_vehicle id_vehicle += 1 vehicles[veh_id] = id_vehicle n_vehicles += 1 else: index = vehicles[veh_id] f.write( str(index) + ":" + str(x2) + ":" + str(y2) + "\n") n_graphs += 1 file_logger.info( "Graph number %s was simulated at %s with %s new vehicles and size %s." % (n_graphs, current_time, n_vehicles, graph_size)) f.write("END " + str(current_time) + " \n") graph_size = 0 traci.close() sys.stdout.flush()
def create_logger_handler(fd, level, max_bytes=10240000, backup_count=5): handler = handlers.RotatingFileHandler(fd, maxBytes=max_bytes, backupCount=backup_count) handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] [%(filename)s] %(message)s')) handler.setLevel(level) return handler
config.read(vv_settings.CONFIG_DIR) """ Logging """ if config['logging'].getboolean('log') is True: logger = logging.getLogger('rest_VariantValidator') # We are setting 2 types of logging. To screen at the level DEBUG console_level = config['logging']['console'].upper() log_console_level = logging.getLevelName(console_level) logger.setLevel(log_console_level) # We will also log to a file # Log with a rotating file-handler. This sets the maximum size of the log to 0.5Mb and allows two additional logs # The logs are then deleted and replaced in rotation logHandler = handlers.RotatingFileHandler('rest_VariantValidator.log', maxBytes=500000, backupCount=2) # We want to minimise the amount of information we log to capturing bugs file_level = config['logging']['file'].upper() log_file_level = logging.getLevelName(file_level) logHandler.setLevel(log_file_level) logger.addHandler(logHandler) """ Create a parser object locally """ parser = request_parser.parser # Define the application as a Flask app with the name defined by __name__ (i.e. the name of the current module) # Most tutorials define application as "app", but I have had issues with this when it comes to deployment, # so application is recommended application = Flask(__name__)
from collections import defaultdict from itertools import chain from FetchGene import Myinfo from pyUniprot import * from DomainCds import * from urllib.error import HTTPError from xml.parsers.expat import ExpatError from logging import handlers logger = logging.getLogger() handler = logging.StreamHandler() fh = handlers.RotatingFileHandler('mRNA_annotation.log', mode='a+', encoding="utf-8", maxBytes=5 * 1024 * 1024, backupCount=2, delay=0) fh.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.addHandler(fh) logger.setLevel(logging.DEBUG) class AttrDict(dict): def __init__(self): dict.__init__(self)
def singleLogger(elementName, level=None, filename=None): #file writing handler producer = Producer() HOMEPATH = Library.getHomepath() global emulationEndLogger emulationEndLogger = Library.loggerSet("Logger") def logLevelGet(): LOG_LEVEL = logging.INFO LogLevel = Library.readLogLevel("coreloglevel") if LogLevel == "info": LOG_LEVEL = logging.INFO if LogLevel == "debug": LOG_LEVEL = logging.DEBUG else: LOG_LEVEL = logging.INFO return LOG_LEVEL if level == None: level = logLevelGet() fileLogger = logging.getLogger(elementName) fileLogger.setLevel(level) #we do not add additional handlers if they are there if not len(fileLogger.handlers): #adding producer handler #bHandler= EMQproducer.BroadcastLogHandler(elementName,producer) #fileLogger.addHandler(bHandler) #EMQproducer.StreamAndBroadcastHandler("TEST",producer) if filename == None: #setting log rotation for 10 files each up to 10000000 bytes (10MB) fileHandler = handlers.RotatingFileHandler( HOMEPATH + "/logs/COCOMAlogfile.csv", 'a', 10000000, 10) fileLoggerFormatter = logging.Formatter( '%(asctime)s;%(name)s;%(levelname)s;%(message)s', datefmt='%m/%d/%Y %H:%M:%S') fileHandler.setFormatter(fileLoggerFormatter) fileLogger.addHandler(fileHandler) #cli writing handler cliLoggerFormatter = logging.Formatter( '%(asctime)s - [%(name)s] - %(levelname)s : %(message)s', datefmt='%m/%d/%Y %H:%M:%S') cliHandler = logging.StreamHandler() cliHandler.setFormatter(cliLoggerFormatter) fileLogger.addHandler(cliHandler) else: fileHandler = logging.FileHandler(HOMEPATH + "/logs/" + str(filename)) fileLoggerFormatter = logging.Formatter( '%(asctime)s;%(name)s;%(levelname)s;%(message)s', datefmt='%m/%d/%Y %H:%M:%S') fileHandler.setFormatter(fileLoggerFormatter) fileLogger.addHandler(fileHandler) return fileLogger
LOGFILE = "/tmp/edacom.log" class MWALogFormatter(logging.Formatter): def format(self, record): return "%s: time %10.6f - %s" % (record.levelname, time.time(), record.getMessage()) mwalf = MWALogFormatter() logger = logging.getLogger() logger.setLevel(logging.DEBUG) fh = handlers.RotatingFileHandler( LOGFILE, maxBytes=1000000000, backupCount=5) # 1 Gb per file, max of five old log files fh.setLevel(LOGLEVEL_LOGFILE) fh.setFormatter(mwalf) ch = logging.StreamHandler() ch.setLevel(LOGLEVEL_CONSOLE) ch.setFormatter(mwalf) # rh = handlers.SysLogHandler(address=('mw-gw', 514)) # rh.setLevel(LOGLEVEL_REMOTE) # rh.setFormatter(mwalf) # add the handlers to the logger logger.addHandler(fh) logger.addHandler(ch)
'%(filename)-24s:' '%(name)-24s:' '%(levelname)-10s:' '%(funcName)-24s:' '%(lineno)-4d:' '%(message)s') stream_formatter = logging.Formatter('%(levelname)-10s:' '%(name)-20s:' '%(message)s') stream_handler = logging.StreamHandler() stream_handler.setFormatter(stream_formatter) stream_handler.setLevel(logging.INFO) tests_handler = handlers.RotatingFileHandler(TESTS_LOG_PATH, maxBytes=500000, backupCount=1) tests_handler.setFormatter(file_formatter) tests_handler.setLevel(logging.DEBUG) debug_handler = handlers.RotatingFileHandler(DEBUG_LOG_PATH, maxBytes=500000, backupCount=5) debug_handler.setFormatter(file_formatter) debug_handler.setLevel(logging.DEBUG) logger = logging.getLogger('UserChecker') logger.setLevel(logging.DEBUG) logger.addHandler(debug_handler) logger.addHandler(stream_handler)
def init(self, *priv): if not priv: raise SnmpsimError('Bad log file params, need filename') if sys.platform[:3] == 'win': # fix possibly corrupted absolute windows path if len(priv[0]) == 1 and priv[0].isalpha() and len(priv) > 1: priv = [priv[0] + ':' + priv[1]] + list(priv[2:]) maxsize = 0 maxage = None if len(priv) > 1 and priv[1]: try: if priv[1][-1] == 'k': maxsize = int(priv[1][:-1]) * 1024 elif priv[1][-1] == 'm': maxsize = int(priv[1][:-1]) * 1024 * 1024 elif priv[1][-1] == 'g': maxsize = int(priv[1][:-1]) * 1024 * 1024 * 1024 elif priv[1][-1] == 'S': maxage = ('S', int(priv[1][:-1])) elif priv[1][-1] == 'M': maxage = ('M', int(priv[1][:-1])) elif priv[1][-1] == 'H': maxage = ('H', int(priv[1][:-1])) elif priv[1][-1] == 'D': maxage = ('D', int(priv[1][:-1])) else: raise ValueError('Unknown log rotation criterion: %s' % priv[1][-1]) except ValueError: raise SnmpsimError( 'Error in timed log rotation specification. Use ' '<NNN>k,m,g for size or <NNN>S,M,H,D for time limits') try: if maxsize: handler = handlers.RotatingFileHandler(priv[0], backupCount=30, maxBytes=maxsize) elif maxage: handler = self.TimedRotatingFileHandler(priv[0], backupCount=30, when=maxage[0], interval=maxage[1]) else: handler = handlers.WatchedFileHandler(priv[0]) except Exception: raise SnmpsimError('Failure configure logging: %s' % sys.exc_info()[1]) handler.setFormatter(logging.Formatter('%(message)s')) self._logger.addHandler(handler) self('Log file %s, rotation rules: ' '%s' % (priv[0], maxsize and '> %sKB' % (maxsize / 1024) or maxage and '%s%s' % (maxage[1], maxage[0]) or '<none>'))
@author: jack ''' import glob import logging from logging import handlers filename = "log/test.log" logger = logging.getLogger("MyLogger") logger.setLevel(logging.DEBUG) handler = handlers.RotatingFileHandler( filename, maxBytes=10**4, backupCount=10, ) logger.addHandler(handler) def run(logger): for i in range(10**2): logger.debug("i = %d" % i) log_files = glob.glob("%s*" % filename) for i in log_files: print(i) if __name__ == '__main__':
def init_logging(filename: str=None, main_logger='', on_error=None) -> logging.Logger: """Setup the logger and logging handlers. If filename is set, all logs will be written to this file as well. This also sets sys.except_hook, so uncaught exceptions are captured. on_error should be a function to call when this is done (taking type, value, traceback). """ global short_log_format, long_log_format global stderr_loghandler, stdout_loghandler import logging from logging import handlers import sys, io, os class NewLogRecord(logging.getLogRecordFactory()): """Allow passing an alias for log modules.""" # This breaks %-formatting, so only set when init_logging() is called. alias = None # type: str def getMessage(self): """We have to hook here to change the value of .module. It's called just before the formatting call is made. """ if self.alias is not None: self.module = self.alias return str(self.msg) logging.setLogRecordFactory(NewLogRecord) logger = logging.getLogger('BEE2') logger.setLevel(logging.DEBUG) # Put more info in the log file, since it's not onscreen. long_log_format = logging.Formatter( '[{levelname}] {module}.{funcName}(): {message}', style='{', ) # Console messages, etc. short_log_format = logging.Formatter( # One letter for level name '[{levelname[0]}] {module}: {message}', style='{', ) if filename is not None: # Make the directories the logs are in, if needed. os.makedirs(os.path.dirname(filename), exist_ok=True) # The log contains DEBUG and above logs. # We rotate through logs of 500kb each, so it doesn't increase too much. log_handler = handlers.RotatingFileHandler( filename, backupCount=5, ) log_handler.doRollover() log_handler.setLevel(logging.DEBUG) log_handler.setFormatter(long_log_format) logger.addHandler(log_handler) err_log_handler = handlers.RotatingFileHandler( filename[:-3] + 'error.' + filename[-3:], backupCount=5, ) err_log_handler.doRollover() err_log_handler.setLevel(logging.WARNING) err_log_handler.setFormatter(long_log_format) logger.addHandler(err_log_handler) # This is needed for multiprocessing, since it tries to flush stdout. # That'll fail if it is None. class NullStream(io.IOBase): """A stream object that discards all data.""" def __init__(self): super(NullStream, self).__init__() @staticmethod def write(self, *args, **kwargs): pass @staticmethod def read(*args, **kwargs): return '' if sys.stdout: stdout_loghandler = logging.StreamHandler(sys.stdout) # When run from source, dump debug output. stdout_loghandler.setLevel(logging.DEBUG if DEV_MODE else logging.INFO) stdout_loghandler.setFormatter(long_log_format) logger.addHandler(stdout_loghandler) if sys.stderr: def ignore_warnings(record: logging.LogRecord): """Filter out messages higher than WARNING. Those are handled by stdError, and we don't want duplicates. """ return record.levelno < logging.WARNING stdout_loghandler.addFilter(ignore_warnings) else: sys.stdout = NullStream() if sys.stderr: stderr_loghandler = logging.StreamHandler(sys.stderr) stderr_loghandler.setLevel(logging.WARNING) stderr_loghandler.setFormatter(long_log_format) logger.addHandler(stderr_loghandler) else: sys.stderr = NullStream() # Use the exception hook to report uncaught exceptions, and finalise the # logging system. old_except_handler = sys.excepthook def except_handler(exc_type, exc_value, exc_tb): """Log uncaught exceptions.""" if not issubclass(exc_type, Exception): # It's subclassing BaseException (KeyboardInterrupt, SystemExit), # so we should quit without messages. logging.shutdown() return logger._log( level=logging.ERROR, msg='Uncaught Exception:', args=(), exc_info=(exc_type, exc_value, exc_tb), ) logging.shutdown() if on_error is not None: on_error(exc_type, exc_value, exc_tb) # Call the original handler - that prints to the normal console. old_except_handler(exc_type, exc_value, exc_tb) sys.excepthook = except_handler if main_logger: return getLogger(main_logger) else: return LoggerAdapter(logger)
def init_logger(screenlevel, filename=None, logdir=None, modulename=''): """ Initialize Logger. Args: screenlevel (logging): logging.INFO or logging.DEBUG filename (str): filename (without .log) logdir (str): directory name for log modulename (str): project name default """ # for all other modules just use log = logging.getLogger(__name__) try: if not os.path.exists(logdir): os.makedirs(logdir) except OSError: print(f"Creation of the directory '{logdir}' failed") exit(1) else: print(f"Successfully created the directory '{logdir}' ") pics_path = "log/pics" try: if not os.path.exists(pics_path): os.makedirs(pics_path) except OSError: print(f"Creation of the directory '{pics_path}' failed") exit(1) else: print(f"Successfully created the directory '{pics_path}' ") root = logging.getLogger() [root.removeHandler(rh) for rh in root.handlers] # pylint: disable=W0106 [root.removeFilter(rf) for rf in root.filters] # pylint: disable=W0106 root = logging.getLogger('') root.setLevel(logging.WARNING) stream_handler = logging.StreamHandler(sys.stdout) stream_handler.setLevel(screenlevel) if filename and not filename == 'None': filename = filename.replace("{date}", datetime.date.today().strftime("%Y%m%d")) all_logs_filename = os.path.join(logdir, filename + '.log') error_filename = os.path.join(logdir, filename + '_errors.log') info_filename = os.path.join(logdir, filename + '_info.log') print("Saving log file to: {}".format(all_logs_filename)) print("Saving info file to: {}".format(info_filename)) print("Saving error only file to: {}".format(error_filename)) file_handler2 = handlers.RotatingFileHandler(all_logs_filename, maxBytes=300000, backupCount=20) file_handler2.setLevel(logging.DEBUG) error_handler = handlers.RotatingFileHandler(error_filename, maxBytes=300000, backupCount=20) error_handler.setLevel(logging.WARNING) info_handler = handlers.RotatingFileHandler(info_filename, maxBytes=30000000, backupCount=100) info_handler.setLevel(logging.INFO) # formatter when using --log command line and writing log to a file file_handler2.setFormatter( logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s')) error_handler.setFormatter( logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s')) info_handler.setFormatter( logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s')) # root.addHandler(fh) root.addHandler(file_handler2) root.addHandler(error_handler) root.addHandler(info_handler) # screen output formatter stream_handler.setFormatter( logging.Formatter('%(levelname)s - %(message)s')) root.addHandler(stream_handler) mainlogger = logging.getLogger(modulename) mainlogger.setLevel(logging.DEBUG)
try: os.makedirs(conf["log"]["log_path"], exist_ok=True) # Python>3.2 except TypeError: try: os.makedirs(conf["log"]["log_path"]) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir( conf["log"]["log_path"]): pass else: logger.error("Can not create log file {0}: {1}".format( conf["log"]["log_path"], exc)) else: logfile = "{0}.log".format(conf["log"]["log_file"]) fileHandler = handlers.RotatingFileHandler(logfile, mode='a+', maxBytes=(1048576 * 5), backupCount=7) if conf["log"]["format"] != "": fileFormatter = logging.Formatter("{0}".format(conf["log"]["format"])) fileHandler.setFormatter(fileFormatter) else: fileHandler.setFormatter(logFormatter) fileHandler.setLevel(conf["log"]["logging_level"]) logger.addHandler(fileHandler) logger.info("Enabled Log File: {0}".format(logfile)) else: logger.info("Logging to file disabled.") # Override Log level if needed if "logging_level" in conf["log"]: log_level = conf["log"]["logging_level"]
h1.setFormatter(formatter) logger.addHandler(h1) app = wx.App() standard_paths = wx.StandardPaths.Get( ) #Can't do this until you start the wx app info_dir = standard_paths.GetUserLocalDataDir() print(info_dir) if not os.path.exists(info_dir): os.mkdir(info_dir) h2 = handlers.RotatingFileHandler(os.path.join(info_dir, 'mono_feedback.log'), maxBytes=10e6, backupCount=5, delay=True) # h2.setLevel(logging.INFO) h2.setLevel(logging.DEBUG) formatter2 = logging.Formatter( '%(asctime)s - %(name)s - %(threadName)s - %(levelname)s - %(message)s' ) h2.setFormatter(formatter2) logger.addHandler(h2) bpm_x_name = '18ID:BPM:CMono:X' bpm_y_name = '18ID:BPM:CMono:Y' bpm_int_name = '18ID:BPM:CMono:Intensity' mono = 'mono2'
if request.method == 'POST': print(request) return response @app.route('/models', methods=['GET']) def get_models(): models_list = ['Deepbach'] return jsonify(models_list) @app.route('/current_model', methods=['POST', 'PUT']) def current_model_update(): return 'Model is only loaded once' @app.route('/current_model', methods=['GET']) def current_model_get(): return 'DeepBach' if __name__ == '__main__': file_handler = logging_handlers.RotatingFileHandler('app.log', maxBytes=10000, backupCount=5) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) init_app()
def initLogger(console=False, log_dir=False, verbose=False): """ Setup logging for Tautulli. It uses the logger instance with the name 'tautulli'. Three log handlers are added: * RotatingFileHandler: for the file tautulli.log * LogListHandler: for Web UI * StreamHandler: for console (if console) Console logging is only enabled if console is set to True. This method can be invoked multiple times, during different stages of Tautulli. """ # Close and remove old handlers. This is required to reinit the loggers # at runtime log_handlers = logger.handlers[:] + \ logger_api.handlers[:] + \ logger_plex_websocket.handlers[:] + \ cherrypy.log.error_log.handlers[:] for handler in log_handlers: # Just make sure it is cleaned up. if isinstance(handler, handlers.RotatingFileHandler): handler.close() elif isinstance(handler, logging.StreamHandler): handler.flush() logger.removeHandler(handler) logger_api.removeHandler(handler) logger_plex_websocket.removeHandler(handler) cherrypy.log.error_log.removeHandler(handler) # Configure the logger to accept all messages logger.propagate = False logger.setLevel(logging.DEBUG if verbose else logging.INFO) logger_api.propagate = False logger_api.setLevel(logging.DEBUG if verbose else logging.INFO) logger_plex_websocket.propagate = False logger_plex_websocket.setLevel(logging.DEBUG if verbose else logging.INFO) cherrypy.log.error_log.propagate = False # Setup file logger if log_dir: file_formatter = logging.Formatter( '%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s', '%Y-%m-%d %H:%M:%S') # Main Tautulli logger filename = os.path.join(log_dir, FILENAME) file_handler = handlers.RotatingFileHandler(filename, maxBytes=MAX_SIZE, backupCount=MAX_FILES, encoding='utf-8') file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(file_formatter) logger.addHandler(file_handler) cherrypy.log.error_log.addHandler(file_handler) # Tautulli API logger filename = os.path.join(log_dir, FILENAME_API) file_handler = handlers.RotatingFileHandler(filename, maxBytes=MAX_SIZE, backupCount=MAX_FILES, encoding='utf-8') file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(file_formatter) logger_api.addHandler(file_handler) # Tautulli websocket logger filename = os.path.join(log_dir, FILENAME_PLEX_WEBSOCKET) file_handler = handlers.RotatingFileHandler(filename, maxBytes=MAX_SIZE, backupCount=MAX_FILES, encoding='utf-8') file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(file_formatter) logger_plex_websocket.addHandler(file_handler) # Setup console logger if console: console_formatter = logging.Formatter( '%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s', '%Y-%m-%d %H:%M:%S') console_handler = logging.StreamHandler() console_handler.setFormatter(console_formatter) console_handler.setLevel(logging.DEBUG) logger.addHandler(console_handler) cherrypy.log.error_log.addHandler(console_handler) # Add filters to log handlers # Only add filters after the config file has been initialized # Nothing prior to initialization should contain sensitive information if not plexpy.DEV and plexpy.CONFIG: log_handlers = logger.handlers + \ logger_api.handlers + \ logger_plex_websocket.handlers + \ cherrypy.log.error_log.handlers for handler in log_handlers: handler.addFilter(BlacklistFilter()) handler.addFilter(PublicIPFilter()) handler.addFilter(PlexDirectIPFilter()) handler.addFilter(EmailFilter()) handler.addFilter(UsernameFilter()) handler.addFilter(PlexTokenFilter()) # Install exception hooks initHooks()
# open local settings with open('./settings.json') as local_json_file: local_submodule_settings = json.loads(local_json_file.read()) local_json_file.close() # log setup current_script_name = os.path.basename(__file__).split('.')[0] log_path_filename = ''.join( [local_submodule_settings['log_path'], current_script_name, '.log']) logging.basicConfig(filename=log_path_filename, level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(message)s') logger = logging.getLogger(__name__) logHandler = handlers.RotatingFileHandler(log_path_filename, maxBytes=10485760, backupCount=5) logger.addHandler(logHandler) class core_alternative_training(): def imagine(self, question, local_cat_settings): try: answer = question == 42 # first check if experience_memory exist if os.path.isfile(''.join([ local_cat_settings['clean_data_path'], 'experience_memory.npy' ])): # method, group, weights, classes (a form for unknown_data_representation based in previous data) experience_memory = np.load(''.join([
logger = logging.getLogger('web') #把filtter对象添加到logger中 logger.addFilter(IgnoreBackupFilter()) #设置日志级别 logger.setLevel(logging.DEBUG) #生成 handler 对象 ch = logging.StreamHandler() # 输出日志到屏幕 fh = logging.FileHandler('web.log') # 输出日志到文件中 #截取日志,按照每份日志的大小来保存,超出大小后重新生成新的日志 fh1 = handlers.RotatingFileHandler('web.log',maxBytes=10,backupCount=3) #(日志名称,没份日志大小,保存多少份日志) #截取日志,按照时间来截取日志 fh2 = handlers.TimedRotatingFileHandler('web.log',when='s',interval= 2,backupCount=3) #(名称,时间级数,时间长度,保存文件个数) #设置输出日志的级别 ch.setLevel(logging.INFO) fh.setLevel(logging.WARNING) #把handler对象绑定到logger logger.addHandler(ch) logger.addHandler(fh) #生成 formatter 对象
def initLogger(console=False, log_dir=False, verbose=False): """ Setup logging for Plex:CS. It uses the logger instance with the name 'plexcs'. Three log handlers are added: * RotatingFileHandler: for the file plexcs.log * LogListHandler: for Web UI * StreamHandler: for console (if console) Console logging is only enabled if console is set to True. This method can be invoked multiple times, during different stages of Plex:CS. """ # Close and remove old handlers. This is required to reinit the loggers # at runtime for handler in logger.handlers[:]: # Just make sure it is cleaned up. if isinstance(handler, handlers.RotatingFileHandler): handler.close() elif isinstance(handler, logging.StreamHandler): handler.flush() logger.removeHandler(handler) # Configure the logger to accept all messages logger.propagate = False logger.setLevel(logging.DEBUG if verbose else logging.INFO) # Add list logger loglist_handler = LogListHandler() loglist_handler.setLevel(logging.DEBUG) logger.addHandler(loglist_handler) # Setup file logger if log_dir: filename = os.path.join(log_dir, FILENAME) file_formatter = logging.Formatter( '%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S') file_handler = handlers.RotatingFileHandler(filename, maxBytes=MAX_SIZE, backupCount=MAX_FILES) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(file_formatter) logger.addHandler(file_handler) # Setup console logger if console: console_formatter = logging.Formatter( '%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s', '%d-%b-%Y %H:%M:%S') console_handler = logging.StreamHandler() console_handler.setFormatter(console_formatter) console_handler.setLevel(logging.DEBUG) logger.addHandler(console_handler) # Install exception hooks initHooks()
import bme280 config.read('weather.ini') settings = config['SETTINGS'] LOG_FILE = settings["LOG_FILE"] windFinder = config["WINDFINDER"] windGuru = config["WINDGURU"] wUnderground = config["WEATHERUNDERGROUND"] FORMAT = "%(asctime)s %(levelname)s %(message)s " logger = logging.getLogger("weatherd") logger.setLevel(logging.INFO) handler = handlers.RotatingFileHandler(LOG_FILE, mode='a', maxBytes=10000, backupCount=3) formatter = logging.Formatter(FORMAT, "%Y-%m-%d %H:%M:%S") handler.setFormatter(formatter) handler.setLevel(logging.INFO) logger.addHandler(handler) # ************************ # WINDFINDER # ************************ # https://www.windfinder.com/report/phan-rang-kite-center_my-hoa # # Upload request example: # http://www.windfinder.com/wind-cgi/httpload.pl?sender_id=phan-rang-kite-center_my-hoa&password=f47d4b810c1cc74b&date=19.5.2011&time=17:13&airtemp=20&windspeed=12&gust=14&winddir=180&pressure=1012&rain=5 # # sender_id: phan-rang-kite-center_my-hoa
def initialize(self): # Extracts the controller Log Level from the ipop-config file, # If nothing is provided the default is INFO if "LogLevel" in self._cm_config: level = getattr(logging, self._cm_config["LogLevel"]) else: level = getattr(logging, "info") # If the Logging is set to Console by the User if self._cm_config["Device"] == "Console": # Console logging logging.basicConfig(format="[%(asctime)s.%(msecs)03d] %(levelname)s: %(message)s", datefmt="%H:%M:%S", level=level) self._logger = logging.getLogger("IPOP console logger") # If the Logging is set to File by the User elif self._cm_config["Device"] == "File": # Extracts the filepath else sets logs to current working directory filepath = self._cm_config.get("Directory", "./") fqname = filepath + \ self._cm_config.get("CtrlLogFileName", "ctrl.log") if not os.path.exists(filepath): os.makedirs(filepath, exist_ok=True) if os.path.isfile(fqname): os.remove(fqname) self._logger = logging.getLogger("IPOP Rotating Log") self._logger.setLevel(level) # Creates rotating filehandler handler = lh.RotatingFileHandler(filename=fqname, maxBytes=self._cm_config["MaxFileSize"], backupCount=self._cm_config["MaxArchives"]) formatter = logging.Formatter( "[%(asctime)s.%(msecs)03d] %(levelname)s:%(message)s", datefmt="%Y%m%d %H:%M:%S") handler.setFormatter(formatter) # Adds the filehandler to the Python logger module self._logger.addHandler(handler) # If the Logging is set to All by the User else: self._logger = logging.getLogger("IPOP Console & File Logger") self._logger.setLevel(level) #Console Logger console_handler = logging.StreamHandler() console_log_formatter = logging.Formatter( "[%(asctime)s.%(msecs)03d] %(levelname)s: %(message)s", datefmt="%H:%M:%S") console_handler.setFormatter(console_log_formatter) self._logger.addHandler(console_handler) # Extracts the filepath else sets logs to current working directory filepath = self._cm_config.get("Directory", "./") fqname = filepath + \ self._cm_config.get("CtrlLogFileName", "ctrl.log") if not os.path.exists(filepath): os.makedirs(filepath, exist_ok=True) if os.path.isfile(fqname): os.remove(fqname) #File Logger # Creates rotating filehandler file_handler = lh.RotatingFileHandler(filename=fqname) file_log_formatter = logging.Formatter( "[%(asctime)s.%(msecs)03d] %(levelname)s:%(message)s", datefmt="%Y%m%d %H:%M:%S") file_handler.setFormatter(file_log_formatter) self._logger.addHandler(file_handler) self._logger.info("Logger: Module loaded")
timer.start() f.write(stringIO.getvalue()) stringIO = StringIO() logger.info('====process {} writeStringIO costTime {} ms===='.format(os.getpid(), timer.cost())) logger.info('====process {} one sample timeTotal costTime {} ms===='.format(os.getpid(), timerTotal.cost())) f.write(stringIO.getvalue()) f.close() if __name__ == '__main__': todayStr1 = datetime.strftime(datetime.now(), '%Y%m%d') todayStr2 = datetime.strftime(datetime.now(), '%Y-%m-%d') logFileName = '/data/search/predict-2019/script/intl_pricePrediction.log' logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter(fmt='[%(asctime)s] - %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') rotating_file = handlers.RotatingFileHandler(filename=logFileName, mode='a', maxBytes=1024*1024*1024, backupCount=3) rotating_file.setLevel(logging.DEBUG) rotating_file.setFormatter(formatter) logger.addHandler(rotating_file) with open('/data/search/predict-2019/idList_temp.txt', 'r') as f_temp: str_L = f_temp.readline() L = str.split(str_L, ',') L.pop() n = ceil(len(L)/100) List = [L[i:i+n] for i in range(0, len(L), n)] p = Pool(14) for i in range(len(List)): p.apply_async(processRun, args=(List[i],)) p.close() p.join()
import time import uuid from bson.binary import Binary from bson.json_util import dumps from flask import request from flask_classful import FlaskView import pymongo from katana.shared_utils.mongoUtils import mongoUtils from katana.shared_utils.wimUtils import odl_wimUtils, test_wimUtils # Logging Parameters logger = logging.getLogger(__name__) file_handler = handlers.RotatingFileHandler("katana.log", maxBytes=10000, backupCount=5) stream_handler = logging.StreamHandler() formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") stream_formatter = logging.Formatter( "%(asctime)s %(name)s %(levelname)s %(message)s") file_handler.setFormatter(formatter) stream_handler.setFormatter(stream_formatter) logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(stream_handler) class WimView(FlaskView): route_prefix = "/api/" req_fields = ["id", "url", "type"]
def main(self, args): # Do the thing log_file_path = '/var/log/kernelstub.log' if args.log_file: log_file_path = args.log_file verbosity = 0 if args.verbosity: verbosity = args.verbosity if verbosity > 2: verbosity = 2 if args.print_config: verbosity = 1 level = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG, } console_level = level[verbosity] file_level = level[2] stream_fmt = logging.Formatter( '%(name)-21s: %(levelname)-8s %(message)s') file_fmt = logging.Formatter( '%(asctime)s - %(name)-21s: %(levelname)-8s %(message)s') log = logging.getLogger('kernelstub') console_log = logging.StreamHandler() console_log.setFormatter(stream_fmt) console_log.setLevel(console_level) file_log = handlers.RotatingFileHandler(log_file_path, maxBytes=(1048576 * 5), backupCount=5) file_log.setFormatter(file_fmt) file_log.setLevel(file_level) log.addHandler(console_log) log.addHandler(file_log) log.setLevel(logging.DEBUG) log.debug('Got command line options: %s' % args) # Figure out runtime options no_run = False if args.dry_run: no_run = True config = Config.Config() configuration = config.config['user'] if args.esp_path: configuration['esp_path'] = args.esp_path root_path = "/" if args.root_path: root_path = args.root_path opsys = Opsys.OS() if args.kernel_path: log.debug('Manually specified kernel path:\n ' + ' %s' % args.kernel_path) opsys.kernel_path = args.kernel_path else: opsys.kernel_path = os.path.join(root_path, opsys.kernel_name) if args.initrd_path: log.debug('Manually specified initrd path:\n ' + ' %s' % args.initrd_path) opsys.initrd_path = args.initrd_path else: opsys.initrd_path = os.path.join(root_path, opsys.initrd_name) if not os.path.exists(opsys.kernel_path): log.exception('Can\'t find the kernel image! \n\n' 'Please use the --kernel-path option to specify ' 'the path to the kernel image') exit(0) if not os.path.exists(opsys.initrd_path): log.exception('Can\'t find the initrd image! \n\n' 'Please use the --initrd-path option to specify ' 'the path to the initrd image') exit(0) # Check for kernel parameters. Without them, stop and fail if args.k_options: configuration['kernel_options'] = self.parse_options( args.k_options.split()) else: try: configuration['kernel_options'] except KeyError: error = ("cmdline was 'InvalidConfig'\n\n" "Could not find any valid configuration. This " "probably means that the configuration file is " "corrupt. Either remove it to regenerate it from" "default or fix the existing one.") log.exception(error) raise CmdLineError("No Kernel Parameters found") exit(168) log.debug(config.print_config()) if args.preserve_live and configuration['live_mode']: configuration['live_mode'] = True log.warning( 'Live mode is enabled!\n' 'Kernelstub is running in live environment mode. This usually ' 'means that you are running a live disk, and kernelstub should ' 'not run. We are thus exiting with 0.\n' 'If you are not running a live disk, please run ' '`sudo kernelstub` to disable live mode.') exit(0) configuration['live_mode'] = False if args.setup_loader: configuration['setup_loader'] = True if args.off_loader: configuration['setup_loader'] = False if args.install_stub: configuration['manage_mode'] = False if args.manage_mode: configuration['manage_mode'] = True log.debug('Checking configuration integrity...') try: kernel_opts = configuration['kernel_options'] esp_path = configuration['esp_path'] setup_loader = configuration['setup_loader'] manage_mode = configuration['manage_mode'] force = configuration['force_update'] except KeyError: log.exception( 'Malformed configuration! \n' 'The configuration we got is bad, and we can\'nt continue. ' 'Please check the config files and make sure they are correct. ' 'If you can\'t figure it out, then deleting them should fix ' 'the errors and cause kernelstub to regenerate them from ' 'Default. \n\n You can use "-vv" to get the configuration used.' ) log.debug('Configuration we got: \n\n%s' % config.print_config()) exit(169) if args.add_options: add_opts = args.add_options.split(" ") add_opts = config.parse_options(add_opts) for opt in add_opts: if opt not in kernel_opts: kernel_opts.append(opt) configuration['kernel_options'] = kernel_opts if args.remove_options: rem_opts = args.remove_options.split(" ") rem_opts = config.parse_options(rem_opts) kernel_opts = list(set(kernel_opts) - set(rem_opts)) configuration['kernel_options'] = kernel_opts if args.force_update: force = True if configuration['force_update'] == True: force = True log.debug('Structing objects') drive = Drive.Drive(root_path=root_path, esp_path=esp_path) nvram = Nvram.NVRAM(opsys.name, opsys.version) installer = Installer.Installer(nvram, opsys, drive) # Log some helpful information, to file and optionally console info = (' OS:..................%s %s\n' % (opsys.name_pretty, opsys.version) + ' Root partition:......%s\n' % drive.root_fs + ' Root FS UUID:........%s\n' % drive.root_uuid + ' ESP Path:............%s\n' % esp_path + ' ESP Partition:.......%s\n' % drive.esp_fs + ' ESP Partition #:.....%s\n' % drive.esp_num + ' NVRAM entry #:.......%s\n' % nvram.os_entry_index + ' Boot Variable #:.....%s\n' % nvram.order_num + ' Kernel Boot Options:.%s\n' % " ".join(kernel_opts) + ' Kernel Image Path:...%s\n' % opsys.kernel_path + ' Initrd Image Path:...%s\n' % opsys.initrd_path + ' Force-overwrite:.....%s\n' % str(force)) log.info('System information: \n\n%s' % info) if args.print_config: all_config = (' ESP Location:..................%s\n' % configuration['esp_path'] + ' Management Mode:...............%s\n' % configuration['manage_mode'] + ' Install Loader configuration:..%s\n' % configuration['setup_loader'] + ' Configuration version:.........%s\n' % configuration['config_rev']) log.info('Configuration details: \n\n%s' % all_config) exit(0) log.debug('Setting up boot...') kopts = 'root=UUID=%s ro %s' % (drive.root_uuid, " ".join(kernel_opts)) log.debug('kopts: %s' % kopts) installer.setup_kernel(kopts, setup_loader=setup_loader, overwrite=force, simulate=no_run) try: installer.backup_old(kopts, setup_loader=setup_loader, simulate=no_run) except Exception as e: log.debug( 'Couldn\'t back up old kernel. \nThis might just mean ' + 'You don\'t have an old kernel installed. If you do, try ' + 'with -vv to see debuging information') log.debug(e) installer.copy_cmdline(simulate=no_run) if not manage_mode: installer.setup_stub(kopts, simulate=no_run) log.debug('Saving configuration to file') config.config['user'] = configuration config.save_config() log.debug('Setup complete!\n\n') return 0
shell=True).decode() app = Flask(__name__, instance_path=os.getcwd()) app.config.from_object(__name__) # load config from this file app.config.update( dict(SECRET_KEY='development key', USERNAME='******', PASSWORD='******', UPLOAD_FOLDER=os.path.join(app.instance_path, 'upload'))) socketio = SocketIO(app) root = logging.getLogger() stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.WARN) file_handler = handlers.RotatingFileHandler('logs/service.log', maxBytes=1024 * 1000, backupCount=5) file_handler.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') stream_handler.setFormatter(formatter) file_handler.setFormatter(formatter) root.addHandler(stream_handler) root.addHandler(file_handler) # TODO: Wrap up the upload functionality. ALLOWED_EXTENSIONS = {'xml', 'xslt'} IGNORED_FILES = {'.gitignore'} def upload_file(form_data):
from shutil import copyfile, rmtree import threading import subprocess from dateutil.tz import tzlocal from pytz import timezone import calendar logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) form = logging.Formatter("%(levelname)s - %(message)s") form2 = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") ch = logging.StreamHandler(sys.stdout) ch.setFormatter(form2) logger.addHandler(ch) fh = handlers.RotatingFileHandler(os.environ.get("LOG_FILENAME", "/tmp/log.log"), maxBytes=(1048576 * 5), backupCount=7) fh.setFormatter(form2) logger.addHandler(fh) # ============================================================= STORAGE_DIR = os.environ.get("SAVE_STORAGE", "/tmp") TMP_FOLDER = os.path.join(os.environ.get("TMP_FOLDER", "/tmp"), "hls") HLS_FRAGMENT_TIME = int(os.environ.get("HLS_FRAGMENT", "60")) SAVE_MAX_TIME = int(os.environ.get("SAVE_MAX_TIME", "600")) NAME_LOCALHOST = os.environ.get("NAME_LOCALHOST", "http://127.0.0.1") MODE_DEBUG = os.environ.get("MODE_DEBUG", "False") SCREEN_MIN_DELAY = int(os.environ.get("SCREEN_MIN_DELAY", "5")) MIN_SCREEN_START_TIME = int(os.environ.get("MIN_SCREEN_START_TIM", "0")) CURRENT_TIMEZONE = os.environ.get("CURRENT_TIMEZONE", "Europe/London") chunk_dir = "chunks"
if not os.path.exists(LOG_DIR): try: os.makedirs(LOG_DIR) except OSError: raise LOG_FILE_PATH = os.path.join(LOG_DIR, LOG_FILE_NAME) # This check *must* be executed before logging.basicConfig because, at least on Windows, # basicConfig creates a lock on the log file that prevents renaming. Possibly a workaround # but putting this first seems to dodge the issue if os.path.isfile(LOG_FILE_PATH): # Create a transient handler to do the rollover for us on startup. This won't # be added to the logger as a handler... just used to roll the log on startup. rollover_handler = handlers.RotatingFileHandler(LOG_FILE_PATH, backupCount=10, maxBytes=100 * 1024 * 1024) # Prior log file exists, so roll it to get a clean log for this run try: rollover_handler.doRollover() except Exception: # Eat it since it's *probably* non-fatal and since we're *probably* still able to log to the prior file pass logging.basicConfig( filename=LOG_FILE_PATH, level=logging.DEBUG, format=FORMAT, ) log = logging.getLogger("fairgame")
import tempfile import logging import logging.handlers as handlers from db import ImageFilesTable from images import ImageInfo from config import Config log = logging.getLogger('midb') h = logging.StreamHandler(stream=sys.stderr) h.setFormatter(logging.Formatter('%(name)s [%(levelname)s]: %(message)s')) log.addHandler(h) h = handlers.RotatingFileHandler(Config.LOGFILE, maxBytes=10 * 1024 * 1024, backupCount=10) h.setFormatter( logging.Formatter('%(asctime)s %(name)s [%(levelname)s]: %(message)s')) log.addHandler(h) log.setLevel(logging.DEBUG) def processImageDir(root=None, forceUpdateExisting=False): """Process all images in all subdirectories of the specified root, or Config.IMG_ROOT if root is None. Store their info in the database. If forceUpdateExisting is True, re-process images that are already in the database; otherwise skip them. """ log.info('*** Process Image Directory: session begin ***')