Python logging.Formatter() Examples
The following are 30
code examples of logging.Formatter().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
logging
, or try the search function
.
Example #1
Source File: utilities.py From incubator-spot with Apache License 2.0 | 25 votes |
def get_logger(cls,logger_name,create_file=False): # create logger for prd_ci log = logging.getLogger(logger_name) log.setLevel(level=logging.INFO) # create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') if create_file: # create file handler for logger. fh = logging.FileHandler('SPOT.log') fh.setLevel(level=logging.DEBUG) fh.setFormatter(formatter) # reate console handler for logger. ch = logging.StreamHandler() ch.setLevel(level=logging.DEBUG) ch.setFormatter(formatter) # add handlers to logger. if create_file: log.addHandler(fh) log.addHandler(ch) return log
Example #2
Source File: config.py From Pytorch-Project-Template with MIT License | 12 votes |
def setup_logging(log_dir): log_file_format = "[%(levelname)s] - %(asctime)s - %(name)s - : %(message)s in %(pathname)s:%(lineno)d" log_console_format = "[%(levelname)s]: %(message)s" # Main logger main_logger = logging.getLogger() main_logger.setLevel(logging.INFO) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) console_handler.setFormatter(Formatter(log_console_format)) exp_file_handler = RotatingFileHandler('{}exp_debug.log'.format(log_dir), maxBytes=10**6, backupCount=5) exp_file_handler.setLevel(logging.DEBUG) exp_file_handler.setFormatter(Formatter(log_file_format)) exp_errors_file_handler = RotatingFileHandler('{}exp_error.log'.format(log_dir), maxBytes=10**6, backupCount=5) exp_errors_file_handler.setLevel(logging.WARNING) exp_errors_file_handler.setFormatter(Formatter(log_file_format)) main_logger.addHandler(console_handler) main_logger.addHandler(exp_file_handler) main_logger.addHandler(exp_errors_file_handler)
Example #3
Source File: util.py From mlbv with GNU General Public License v3.0 | 10 votes |
def init_logging(log_file=None, append=False, console_loglevel=logging.INFO): """Set up logging to file and console.""" if log_file is not None: if append: filemode_val = 'a' else: filemode_val = 'w' logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s", # datefmt='%m-%d %H:%M', filename=log_file, filemode=filemode_val) # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(console_loglevel) # set a format which is simpler for console use formatter = logging.Formatter("%(message)s") console.setFormatter(formatter) # add the handler to the root logger logging.getLogger('').addHandler(console) global LOG LOG = logging.getLogger(__name__)
Example #4
Source File: conftest.py From drydock with Apache License 2.0 | 9 votes |
def setup_logging(): # Setup root logger logger = logging.getLogger('drydock') logger.setLevel('DEBUG') ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s - %(message)s' ) ch.setFormatter(formatter) logger.addHandler(ch) # Specalized format for API logging logger = logging.getLogger('drydock.control') logger.propagate = False formatter = logging.Formatter( "%(asctime)s - %(levelname)s - %(user)s - %(req_id)s" " - %(external_ctx)s - %(end_user)s - %(message)s" ) ch = logging.StreamHandler() ch.setFormatter(formatter) logger.addHandler(ch)
Example #5
Source File: a3c.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 8 votes |
def log_config(log_dir=None, log_file=None, prefix=None, rank=0): reload(logging) head = '%(asctime)-15s Node[' + str(rank) + '] %(message)s' if log_dir: logging.basicConfig(level=logging.DEBUG, format=head) if not os.path.exists(log_dir): os.makedirs(log_dir) if not log_file: log_file = (prefix if prefix else '') + datetime.now().strftime('_%Y_%m_%d-%H_%M.log') log_file = log_file.replace('/', '-') else: log_file = log_file log_file_full_name = os.path.join(log_dir, log_file) handler = logging.FileHandler(log_file_full_name, mode='w') formatter = logging.Formatter(head) handler.setFormatter(formatter) logging.getLogger().addHandler(handler) logging.info('start with arguments %s', args) else: logging.basicConfig(level=logging.DEBUG, format=head) logging.info('start with arguments %s', args)
Example #6
Source File: getmetrics_cadvisor.py From InsightAgent with Apache License 2.0 | 7 votes |
def set_logger_config(level): """ set up logging according to the defined log level """ # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #7
Source File: utils.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def create_logger(name): """ Create a logger object with the given name. If this is the first time that we call this method, then initialize the formatter. """ base = logging.getLogger("cleverhans") if len(base.handlers) == 0: ch = logging.StreamHandler() formatter = logging.Formatter('[%(levelname)s %(asctime)s %(name)s] ' + '%(message)s') ch.setFormatter(formatter) base.addHandler(ch) return base
Example #8
Source File: benchmark.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def setup_logging(log_loc): if os.path.exists(log_loc): shutil.move(log_loc, log_loc + "_" + str(int(os.path.getctime(log_loc)))) os.makedirs(log_loc) log_file = '{}/benchmark.log'.format(log_loc) LOGGER = logging.getLogger('benchmark') LOGGER.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s') file_handler = logging.FileHandler(log_file) console_handler = logging.StreamHandler() file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) LOGGER.addHandler(file_handler) LOGGER.addHandler(console_handler) return LOGGER
Example #9
Source File: util.py From jawfish with MIT License | 6 votes |
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 #
Example #10
Source File: config.py From jawfish with MIT License | 6 votes |
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 as 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) style = config.get('style', '%') result = logging.Formatter(fmt, dfmt, style) return result
Example #11
Source File: logger.py From PickTrue with MIT License | 6 votes |
def __get_logger(name): __log_level = logging.INFO if "--debug-%s" % name in sys.argv: __log_level = logging.DEBUG fmt = "%(levelname)s - %(asctime)-15s - %(filename)s - line %(lineno)d --> %(message)s" date_fmt = "%a %d %b %Y %H:%M:%S" formatter = logging.Formatter(fmt, date_fmt) handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger(name) logger.addHandler( handler ) logger.setLevel(level=__log_level) return logger
Example #12
Source File: ColorLogger.py From lichess-bot with GNU Affero General Public License v3.0 | 6 votes |
def enable_color_logging(debug_lvl=logging.DEBUG): if platform.system() == 'Windows': # Windows does not support ANSI escapes and we are using API calls to set the console color logging.StreamHandler.emit = add_coloring_to_emit_windows(logging.StreamHandler.emit) else: # all non-Windows platforms are supporting ANSI escapes so we use them logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit) root = logging.getLogger() root.setLevel(debug_lvl) ch = logging.StreamHandler(sys.stdout) ch.setLevel(debug_lvl) # FORMAT from https://github.com/xolox/python-coloredlogs FORMAT = '%(asctime)s %(name)s[%(process)d] \033[1m%(levelname)s\033[0m %(message)s' formatter = logging.Formatter(FORMAT, "%Y-%m-%d %H:%M:%S") ch.setFormatter(formatter)
Example #13
Source File: client.py From jsonrpcclient with MIT License | 6 votes |
def basic_logging(self) -> None: """ Call this on the client object to create log handlers to output request and response messages. """ # Request handler if len(request_log.handlers) == 0: request_handler = logging.StreamHandler() request_handler.setFormatter( logging.Formatter(fmt=self.DEFAULT_REQUEST_LOG_FORMAT) ) request_log.addHandler(request_handler) request_log.setLevel(logging.INFO) # Response handler if len(response_log.handlers) == 0: response_handler = logging.StreamHandler() response_handler.setFormatter( logging.Formatter(fmt=self.DEFAULT_RESPONSE_LOG_FORMAT) ) response_log.addHandler(response_handler) response_log.setLevel(logging.INFO)
Example #14
Source File: getmetrics_datadog.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #15
Source File: main.py From dogTorch with MIT License | 6 votes |
def setup_logging(filepath, verbose): logFormatter = logging.Formatter( '%(levelname)s %(asctime)-20s:\t %(message)s') rootLogger = logging.getLogger() if verbose: rootLogger.setLevel(logging.DEBUG) else: rootLogger.setLevel(logging.INFO) logging.getLogger('PIL').setLevel(logging.WARNING) # Setup the logger to write into file fileHandler = logging.FileHandler(filepath) fileHandler.setFormatter(logFormatter) rootLogger.addHandler(fileHandler) # Setup the logger to write into stdout consoleHandler = logging.StreamHandler(sys.stdout) consoleHandler.setFormatter(logFormatter) rootLogger.addHandler(consoleHandler)
Example #16
Source File: getmetrics_sysdig.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #17
Source File: config.py From jawfish with MIT License | 6 votes |
def _create_formatters(cp): """Create and return formatters""" flist = cp["formatters"]["keys"] if not len(flist): return {} flist = flist.split(",") flist = _strip_spaces(flist) formatters = {} for form in flist: sectname = "formatter_%s" % form fs = cp.get(sectname, "format", raw=True, fallback=None) dfs = cp.get(sectname, "datefmt", raw=True, fallback=None) c = logging.Formatter class_name = cp[sectname].get("class") if class_name: c = _resolve(class_name) f = c(fs, dfs) formatters[form] = f return formatters
Example #18
Source File: getmetrics_hadoop.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #19
Source File: getlogs_k8s.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """ set up logging according to the defined log level """ # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s [pid %(process)d] %(levelname)-8s %(module)s.%(funcName)s():%(lineno)d %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #20
Source File: log_conf.py From redditswapbot with GNU General Public License v3.0 | 6 votes |
def getLogger(name=None): #configure logging LoggerManager._loggers[name] = logging.getLogger(name) LoggerManager._loggers[name].setLevel(logging.INFO) if logging_dest == 'mysql': db = {'host':mysql_hostname, 'port':3306, 'dbuser':mysql_username, 'dbpassword':mysql_password, 'dbname':mysql_database} sqlh = mySQLHandler.mySQLHandler(db) LoggerManager._loggers[name].addHandler(sqlh) else: fileh = logging.FileHandler('actions.log') fileh.setFormatter(logging.Formatter('%(asctime)s - %(module)s - %(message)s')) LoggerManager._loggers[name].addHandler(fileh) requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) return LoggerManager._loggers[name]
Example #21
Source File: getlogs_spark.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """ set up logging according to the defined log level """ # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #22
Source File: collectdReportMetrics.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #23
Source File: getmetrics_hbase.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #24
Source File: getmetrics_opentsdb.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #25
Source File: get_metadata.py From InsightAgent with Apache License 2.0 | 6 votes |
def __init__(self, *args): """ Implements an interface that allows topology/metadata data to be sent to InsightFinder. """ self._get_parameters() #Set up logging self.logger = logging.getLogger(__name__) # create a file handler handler = logging.StreamHandler(stream=sys.stdout) handler.setLevel(self.log_level) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # add the handlers to the logger self.logger.addHandler(handler) self.logger.setLevel(self.log_level) self._get_agent_config_vars() self._get_reporting_config_vars()
Example #26
Source File: utils.py From calmjs with GNU General Public License v2.0 | 6 votes |
def enable_pretty_logging(logger='calmjs', level=logging.DEBUG, stream=None): """ Shorthand to enable pretty logging """ def cleanup(): logger.removeHandler(handler) logger.level = old_level if not isinstance(logger, logging.Logger): logger = logging.getLogger(logger) old_level = logger.level handler = logging.StreamHandler(stream) handler.setFormatter(logging.Formatter( u'%(asctime)s %(levelname)s %(name)s %(message)s')) logger.addHandler(handler) logger.setLevel(level) return cleanup
Example #27
Source File: reportMetrics.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.INFO logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #28
Source File: insightagent-boilerplate.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """ set up logging according to the defined log level """ # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #29
Source File: getmessages_prometheus.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """ set up logging according to the defined log level """ # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s [pid %(process)d] %(levelname)-8s %(module)s.%(funcName)s():%(lineno)d %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj
Example #30
Source File: getmetrics_zipkin.py From InsightAgent with Apache License 2.0 | 6 votes |
def set_logger_config(level): """Set up logging according to the defined log level""" # Get the root logger logger_obj = logging.getLogger(__name__) # Have to set the root logger level, it defaults to logging.WARNING logger_obj.setLevel(level) # route INFO and DEBUG logging to stdout from stderr logging_handler_out = logging.StreamHandler(sys.stdout) logging_handler_out.setLevel(logging.DEBUG) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(process)d - %(threadName)s - %(levelname)s - %(message)s') logging_handler_out.setFormatter(formatter) logger_obj.addHandler(logging_handler_out) logging_handler_err = logging.StreamHandler(sys.stderr) logging_handler_err.setLevel(logging.WARNING) logger_obj.addHandler(logging_handler_err) return logger_obj