我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用logging.Formatter()。
def configure_logging(path_to_log_directory): """ Configure logger :param path_to_log_directory: path to directory to write log file in :return: """ log_filename = datetime.datetime.now().strftime('%Y-%m-%d') + '.log' importer_logger = logging.getLogger('importer_logger') importer_logger.setLevel(LOG_LEVEL) formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s') fh = logging.FileHandler(filename=os.path.join(path_to_log_directory, log_filename)) fh.setLevel(LOG_LEVEL) fh.setFormatter(formatter) importer_logger.addHandler(fh) sh = logging.StreamHandler(sys.stdout) sh.setLevel(LOG_LEVEL) sh.setFormatter(formatter) importer_logger.addHandler(sh)
def create_logger(): """ Setup the logging environment """ log = logging.getLogger() # root logger log.setLevel(logging.INFO) format_str = '%(asctime)s - %(levelname)-8s - %(message)s' date_format = '%Y-%m-%d %H:%M:%S' if HAVE_COLORLOG and os.isatty(2): cformat = '%(log_color)s' + format_str colors = {'DEBUG': 'reset', 'INFO': 'reset', 'WARNING': 'bold_yellow', 'ERROR': 'bold_red', 'CRITICAL': 'bold_red'} formatter = colorlog.ColoredFormatter(cformat, date_format, log_colors=colors) else: formatter = logging.Formatter(format_str, date_format) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) log.addHandler(stream_handler) return logging.getLogger(__name__)
def init_logger(self, args): level = logging.INFO if args.verbose: level = logging.VERBOSE if args.debug: level = logging.DEBUG logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', level=level) Rthandler = RotatingFileHandler('arbitrage.log', maxBytes=100*1024*1024,backupCount=10) Rthandler.setLevel(level) formatter = logging.Formatter('%(asctime)-12s [%(levelname)s] %(message)s') Rthandler.setFormatter(formatter) logging.getLogger('').addHandler(Rthandler) logging.getLogger("requests").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING)
def init_logging(logfile): formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S' ) fh = logging.FileHandler(logfile) # ch = logging.StreamHandler() fh.setFormatter(formatter) # ch.setFormatter(formatter) # fh.setLevel(logging.INFO) # ch.setLevel(logging.INFO) # logging.getLogger().addHandler(ch) logging.getLogger().addHandler(fh) logging.getLogger().setLevel(logging.INFO) return logging # prepare logging.
def get(cls, tag): """ Get the global logger instance for the given tag :param tag: the log tag :return: the logger instance """ if tag not in cls._LOGGERS: if cls._use_color: handler = colorlog.StreamHandler() handler.setFormatter(colorlog.ColoredFormatter( \ ' %(log_color)s%(name)s/%(levelname)-8s%(reset)s |' ' %(log_color)s%(message)s%(reset)s')) else: handler = logging.StreamHandler() handler.setFormatter(logging.Formatter( \ ' %(name)s/%(levelname)-8s | %(message)s')) logger = logging.getLogger(tag) logger.addHandler(handler) cls._LOGGERS[tag] = logger return cls._LOGGERS[tag]
def __init__(self,loggerName,logFileName,doConsoleLogging): #Formatage log default_formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: '+loggerName+' :: %(message)s') console_formatter = logging.Formatter('%(levelname)s :: %(message)s') self.mainLogger = logging.getLogger('logger.'+logFileName+'') #on nomme le logger self.mainLogger.setLevel(logging.DEBUG) #on met le niveau du logger sur DEBUG, comme ca il ecrit tout #Log vers la console if doConsoleLogging: console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(console_formatter) self.mainLogger.addHandler(console_handler) # creation d'un handler qui va rediriger une ecriture du log vers # un fichier en mode 'append', avec 1 backup et une taille max de 1Mo file_handler = RotatingFileHandler('/mnt/Usb-Solo-Mapper/Logs/'+logFileName+'.log', 'a', 1000000, 1) # on lui met le niveau sur DEBUG, on lui dit qu'il doit utiliser le formateur # cree precedement et on ajoute ce handler au logger file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(default_formatter) self.mainLogger.addHandler(file_handler)
def init_logger(logger_name): # initialize logger log = logging.getLogger(logger_name) _h = logging.FileHandler('%s/%s' % ( cfg.CONF.service.service_log_path, cfg.CONF.service.service_log_filename)) _h.setFormatter(logging.Formatter("'%(asctime)s - %(pathname)s:" "%(lineno)s - %(levelname)s" " - %(message)s'")) log.addHandler(_h) if cfg.CONF.service.enable_debug_log_entries: log.setLevel(logging.DEBUG) else: log.setLevel(logging.INFO) return log
def configure_logging(self): """ Configure logging to log to std output as well as to log file """ log_level = logging.DEBUG log_filename = datetime.now().strftime('%Y-%m-%d') + '.log' sp_logger = logging.getLogger('sp_logger') sp_logger.setLevel(log_level) formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(message)s') fh = logging.FileHandler(filename=self.log_dir + log_filename) fh.setLevel(log_level) fh.setFormatter(formatter) sp_logger.addHandler(fh) sh = logging.StreamHandler(sys.stdout) sh.setLevel(log_level) sh.setFormatter(formatter) sp_logger.addHandler(sh)
def add_stderr_logger(level=logging.DEBUG): """ Helper for quickly adding a StreamHandler to the logger. Useful for debugging. Returns the handler after adding it. """ # This method needs to be in this __init__.py to get the __name__ correct # even if urllib3 is vendored within another package. logger = logging.getLogger(__name__) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) logger.addHandler(handler) logger.setLevel(level) logger.debug('Added a stderr logging handler to logger: %s', __name__) return handler # ... Clean up.
def configureLogging(level, console, file): logger = logging.getLogger() logger.setLevel(level) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') if console: cons = logging.StreamHandler() cons.setLevel(level) cons.setFormatter(formatter) logger.addHandler(cons) print("logging to console") if file: f = logging.FileHandler(file) f.setLevel(level) f.setFormatter(formatter) logger.addHandler(f) print("logging to file {0}".format(file))
def setup_logging(): '''Sets up internal logging. Run this once at startup.''' logger = logging.getLogger() formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') if options.log.filename: handler = logging.handlers.TimedRotatingFileHandler(filename=options.log.filename, when=options.log.when, backupCount=options.log.backup_count, utc=True) handler.setFormatter(formatter) logger.addHandler(handler) if not options.main.daemon: handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) level = getattr(logging, options.log.level.upper()) logger.setLevel(level)
def main(): # Set up a console logger. console = logging.StreamHandler() formatter = logging.Formatter("%(asctime)s %(name)-12s:%(levelname)-8s: %(message)s") console.setFormatter(formatter) logging.getLogger().addHandler(console) logging.getLogger().setLevel(logging.INFO) kw = {} longopts = ['domainname=', 'verbose'] opts, args = getopt.getopt(sys.argv[1:], 'v', longopts) for opt, val in opts: if opt == '--domainname': kw['domainName'] = val if opt in ['-v', '--verbose']: kw['verbose'] = True a = QApplication(sys.argv) QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()")) w = BrowseWindow(**kw) w.show() a.exec_()
def log_to_stderr(level=None): ''' Turn on logging and add a handler which prints to stderr ''' global _log_to_stderr import logging logger = get_logger() formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT) handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) if level: logger.setLevel(level) _log_to_stderr = True return _logger # # Function returning a temp directory which will be removed on exit #
def configure_formatter(self, config): """Configure a formatter from a dictionary.""" if '()' in config: factory = config['()'] # for use in exception handler try: result = self.configure_custom(config) except TypeError, te: if "'format'" not in str(te): raise #Name of parameter changed from fmt to format. #Retry with old name. #This is so that code can be used with older Python versions #(e.g. by Django) config['fmt'] = config.pop('format') config['()'] = factory result = self.configure_custom(config) else: fmt = config.get('format', None) dfmt = config.get('datefmt', None) result = logging.Formatter(fmt, dfmt) return result
def instantiate(p): print("*** instantiate ***") print(p) with rlock: global logger logger = logging.getLogger("freepydius-logger") logger.setLevel(logging.INFO) handler = TimedRotatingFileHandler(_LOG_FILE, when="midnight", interval=1) formatter = logging.Formatter("%(asctime)s %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) log = Log("INSTANCE") log.log(( ('Response', 'created'), )) # return 0 for success or -1 for failure return 0
def main(): """ Called as a console_scripts entry point in setup.py. This function defines the manubot command line script. """ # Track if message gets logged with severity of error or greater # See https://stackoverflow.com/a/45446664/4651668 error_handler = errorhandler.ErrorHandler() # Log to stderr logger = logging.getLogger() stream_handler = logging.StreamHandler(stream=sys.stderr) stream_handler.setFormatter(logging.Formatter('## {levelname}\n{message}', style='{')) logger.addHandler(stream_handler) args = parse_arguments() logger.setLevel(getattr(logging, args.log_level)) prepare_manuscript(args) if error_handler.fired: logging.critical('Failure: exiting with code 1 due to logged errors') raise SystemExit(1)
def logger(level, name, logfile): """ Create and configure file and console logging. :param level: console debugging level only. :param name: logger name :param logfile: log destination file name :return: configured logging object """ logger = logging.getLogger(name) console_handler = logging.StreamHandler() console_handler.setLevel(level) file_handler = logging.FileHandler(logfile) file_handler.setLevel(logging.DEBUG) console_formatter = logging.Formatter("[%(levelname)s] %(message)s") file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") console_handler.setFormatter(console_formatter) file_handler.setFormatter(file_formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) return logger
def __syslog_handler_init(self): """ Initialize the syslog handler if it hasn't been """ if self.syslog_handler is None: try: # TODO: /dev/log is Linux-specific. self.syslog_handler = logging.handlers.SysLogHandler( '/dev/log', facility=self.facility) self.syslog_handler.setFormatter( logging.Formatter( fmt='%(name)s %(levelname)-8s %(message)s' ) ) self.logger.addHandler(self.syslog_handler) except: self.__syslog_handler_deinit()
def format(self, record): format_orig = self._fmt if record.levelno == self.plain_log_level: self._fmt = self.plain_format self._style._fmt = self._fmt result = logging.Formatter.format(self, record) self._fmt = format_orig self._style._fmt = self._fmt return result # Add a class to emulate stdout/stderr
def __init__(self, debug=False, logfile=None): logging.Logger.__init__(self, 'VirtualBMC') try: if logfile is not None: self.handler = logging.FileHandler(logfile) else: self.handler = logging.StreamHandler() formatter = logging.Formatter(DEFAULT_LOG_FORMAT) self.handler.setFormatter(formatter) self.addHandler(self.handler) if debug: self.setLevel(logging.DEBUG) else: self.setLevel(logging.INFO) except IOError, e: if e.errno == errno.EACCES: pass
def configure_logging(debug): '''Sets the data kennel logger to appropriate levels of chattiness.''' default_logger = logging.getLogger('') datadog_logger = logging.getLogger('datadog.api') requests_logger = logging.getLogger('requests') if debug: default_logger.setLevel(logging.DEBUG) datadog_logger.setLevel(logging.INFO) requests_logger.setLevel(logging.INFO) else: default_logger.setLevel(logging.INFO) datadog_logger.setLevel(logging.WARNING) requests_logger.setLevel(logging.WARNING) stream_handler = logging.StreamHandler(sys.__stdout__) stream_handler.setLevel(logging.DEBUG) stream_handler.setFormatter(logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')) default_logger.addHandler(stream_handler)
def add_stderr_logger(level=logging.DEBUG): """ Helper for quickly adding a StreamHandler to the logger. Useful for debugging. Returns the handler after adding it. """ # This method needs to be in this __init__.py to get the __name__ correct # even if urllib3 is vendored within another package. logger = logging.getLogger(__name__) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) logger.addHandler(handler) logger.setLevel(level) logger.debug('Added an stderr logging handler to logger: %s' % __name__) return handler # ... Clean up.
def set_logger(self, s): logger = logging.getLogger(self.__class__.__name__) # create console handler and set level to debug if ('logging_file' not in s) or (s['logging_file'] == None): fh = logging.StreamHandler() else: fh = logging.FileHandler(s['logging_file']) if 'logging_level' in s: fh.setLevel(s['logging_level']) else: fh.setLevel(logging.DEBUG) # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # add console stdout or log file to logger logger.addHandler(fh) self.logger = logger # usage: self.logger.debug(msg)
def logger(level='DEBUG', name=""): fmt = '[%(module)s] %(asctime)s %(levelname)-7.7s %(message)s' dfmt = '%Y-%m-%d %H:%M:%S' level = getattr(logging, level, DEBUG) logger = getLogger('services') logger.setLevel(level) fmter = Formatter(fmt, dfmt) del logger.handlers[:] if name: fh = FileHandler(name) fh.setLevel(level) fh.setFormatter(fmter) logger.addHandler(fh) ch = StreamHandler() ch.setLevel(level) ch.setFormatter(fmter) logger.addHandler(ch) logger.propagate = False return logger
def create_logger(): #logging.basicConfig(format='%(levelname)s - %(message)s') logging.basicConfig(format='%(message)s') root = logging.getLogger() root.setLevel(logging.getLevelName('INFO')) #Add handler for standard output (console) any debug+ #ch = logging.StreamHandler(sys.stdout) #ch.setLevel(logging.getLevelName('DEBUG')) #formatter = logging.Formatter('%(message)s') #ch.setFormatter(formatter) #handler = ColorStreamHandler() #handler.setLevel(logging.getLevelName("DEBUG")) #root.addHandler(handler) return root
def run(context, config): config_obj = load_config(config) log_config = config_obj['LOG'] # Configure root logger if logging level is specified in config if log_config['level']: level = getattr(logging, log_config['level']) logger = logging.getLogger() logger.setLevel(level) formatter = logging.Formatter(log_config['format']) sh = logging.StreamHandler() sh.setFormatter(formatter) logger.addHandler(sh) if log_config['setup_structlog']: setup_structlog(sys.stdout.isatty()) shark = SocketShark(config_obj) try: shark.start() except Exception: shark.log.exception('unhandled exception') raise
def __init__(self, **kwargs): super(Feature, self).__init__(**kwargs) self.result_file = "{}/{}.log".format( CONST.__getattribute__('dir_results'), self.case_name) try: module = kwargs['run']['module'] self.logger = logging.getLogger(module) except KeyError: self.__logger.warning( "Cannot get module name %s. Using %s as fallback", kwargs, self.case_name) self.logger = logging.getLogger(self.case_name) handler = logging.StreamHandler() handler.setLevel(logging.WARN) self.logger.addHandler(handler) handler = logging.FileHandler(self.result_file) handler.setLevel(logging.DEBUG) self.logger.addHandler(handler) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) self.logger.addHandler(handler)
def init_logging(): main_logger = logging.getLogger() formatter = logging.Formatter( fmt='%(asctime)s.%(msecs)03d %(levelname)-8s [%(name)s] %(message)s' , datefmt='%Y-%m-%d %H:%M:%S') handler_stream = logging.StreamHandler(sys.stdout) handler_stream.setFormatter(formatter) main_logger.addHandler(handler_stream) if LOG_TO_FILE: handler_file = logging.handlers.RotatingFileHandler("debug.log" , maxBytes = 2**24 , backupCount = 10) handler_file.setFormatter(formatter) main_logger.addHandler(handler_file) main_logger.setLevel(logging.DEBUG) return main_logger # ============================================================================
def setup_logging(app): """Setup logging.""" from logging.handlers import RotatingFileHandler from logging import Formatter log_file_path = app.config.get('LOG_FILE') log_level = app.config.get('LOG_LEVEL', logging.WARN) if log_file_path: # pragma: no cover file_handler = RotatingFileHandler(log_file_path) file_handler.setFormatter(Formatter( '%(name)s:%(levelname)s:[%(asctime)s] %(message)s ' '[in %(pathname)s:%(lineno)d]' )) file_handler.setLevel(log_level) app.logger.addHandler(file_handler) logger = logging.getLogger('pybossa') logger.setLevel(log_level) logger.addHandler(file_handler)
def setuplogger(consolelevel, filename=None, filelevel=None): """ setup the python root logger to log to the console with defined log level. Optionally also log to file with the provided level """ if filelevel == None: filelevel = consolelevel if sys.version.startswith("2.7"): logging.captureWarnings(True) rootlogger = logging.getLogger() rootlogger.setLevel(min(consolelevel, filelevel)) formatter = logging.Formatter('%(asctime)s.%(msecs)03d [%(levelname)s] %(message)s', datefmt='%Y-%m-%dT%H:%M:%S') if filename != None: filehandler = logging.FileHandler(filename) filehandler.setLevel(filelevel) filehandler.setFormatter(formatter) rootlogger.addHandler(filehandler) consolehandler = logging.StreamHandler() consolehandler.setLevel(consolelevel) consolehandler.setFormatter(formatter) rootlogger.addHandler(consolehandler)