本文整理汇总了Python中logging.FileHandler方法的典型用法代码示例。如果您正苦于以下问题:Python logging.FileHandler方法的具体用法?Python logging.FileHandler怎么用?Python logging.FileHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging
的用法示例。
在下文中一共展示了logging.FileHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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
示例2: get_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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('oa.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
示例3: getLogger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def getLogger(fname=None):
format='%(asctime)s:%(process)d:%(threadName)s:%(levelname)s:%(name)s:%(pathname)s %(lineno)d:%(message)s'
formatter = MyFormatter(format)
logger = logging.getLogger('sdx')
if fname:
fh = logging.FileHandler(fname)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler(stream=sys.stdout)
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
示例4: create_file_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def create_file_logger(root, name, file_name="log.txt", timestamp_format="", debug=False):
file_api = FileAPI(root)
timestamp = ""
if timestamp_format:
timestamp = datetime.now().strftime(timestamp_format)
else:
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
folder = name + "_" + timestamp
# prepare folder and file
with file_api.open_with_mkdir(folder + "/" + file_name) as f:
f.write("".encode("utf-8"))
log_root = os.path.join(root, folder)
logger = create_logger(name, debug)
fh = FileHandler(os.path.join(log_root, file_name), encoding="utf-8")
fh.setLevel(_bool_2_level(debug))
logger.addHandler(fh)
# add close method to release resource
logger.close = types.MethodType(__close, logger)
return logger, log_root
示例5: setup_logging
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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
示例6: create_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def create_logger():
logger = logging.getLogger('sgd_svrg')
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s')
fh = logging.FileHandler('experiments.log')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
################################################################################
# Functions below are for benchmark purpose to calcuate expectation, variance of
# gradients per epoch for each parameter. These calculations will be helpful when
# benchmarking SVRG optimization with other optimization techniques, such as SGD.
# Currently it only calculates the expectation, variance for single context but
# can be extended to multi-context in later iterations.
################################################################################
示例7: logging_config
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def logging_config(name=None, level=logging.DEBUG, console_level=logging.DEBUG):
if name is None:
name = inspect.stack()[1][1].split('.')[0]
folder = os.path.join(os.getcwd(), name)
if not os.path.exists(folder):
os.makedirs(folder)
logpath = os.path.join(folder, name + ".log")
print("All Logs will be saved to %s" %logpath)
logging.root.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logfile = logging.FileHandler(logpath)
logfile.setLevel(level)
logfile.setFormatter(formatter)
logging.root.addHandler(logfile)
#TODO Update logging patterns in other files
logconsole = logging.StreamHandler()
logconsole.setLevel(console_level)
logconsole.setFormatter(formatter)
logging.root.addHandler(logconsole)
return folder
示例8: log_config
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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)
示例9: setup_logging
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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)
示例10: train
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def train(session, model, curr_dir, data_train, data_val):
curr_dir = os.path.join(curr_dir, model.algorithm)
bestmodel_dir = os.path.join(curr_dir, 'best_checkpoint')
if not os.path.exists(curr_dir):
os.makedirs(curr_dir)
file_handler = logging.FileHandler(os.path.join(curr_dir, 'log.txt'))
logging.getLogger().addHandler(file_handler)
with open(os.path.join(curr_dir, FLAGS['save_name'] + '.json'), 'w') as f:
json.dump(FLAGS, f)
if not os.path.exists(bestmodel_dir):
os.makedirs(bestmodel_dir)
initialize_model(session, model, curr_dir, expect_exists=False)
model.train(session, curr_dir, bestmodel_dir, data_train, data_val)
示例11: get_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def get_logger(exp_dir):
"""
creates logger instance. writing out info to file and to terminal.
:param exp_dir: experiment directory, where exec.log file is stored.
:return: logger instance.
"""
logger = logging.getLogger('medicaldetectiontoolkit')
logger.setLevel(logging.DEBUG)
log_file = exp_dir + '/exec.log'
hdlr = logging.FileHandler(log_file)
print('Logging to {}'.format(log_file))
logger.addHandler(hdlr)
logger.addHandler(ColorHandler())
logger.propagate = False
return logger
示例12: get_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def get_logger(name=None, args=None, level=INFO, append=False):
"""Get customized logger.
Args:
name: Name of the logger.
level: Level to log.
Returns:
A logger.
"""
logger = logging.getLogger(name)
if name and not getattr(logger, '_init_done', None):
logger._init_done = True
logger.addHandler(_handler)
if args is not None:
fileHandler = None
if append:
fileHandler = logging.FileHandler(args.log, mode='a')
else:
fileHandler = logging.FileHandler(args.log, mode='w')
fileHandler.setFormatter(_Formatter())
logger.addHandler(fileHandler)
logger.setLevel(level)
return logger
示例13: getLogger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
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]
示例14: get_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def get_logger(logpath, filepath, package_files=[], displaying=True, saving=True, debug=False):
logger = logging.getLogger()
if debug:
level = logging.DEBUG
else:
level = logging.INFO
logger.setLevel(level)
if saving:
info_file_handler = logging.FileHandler(logpath, mode="a")
info_file_handler.setLevel(level)
logger.addHandler(info_file_handler)
if displaying:
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
logger.addHandler(console_handler)
logger.info(filepath)
with open(filepath, "r") as f:
logger.info(f.read())
for f in package_files:
logger.info(f)
with open(f, "r") as package_f:
logger.info(package_f.read())
return logger
示例15: modify_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import FileHandler [as 别名]
def modify_logger(logger, log_file):
# refer: https://docs.python.org/3.5/library/logging.html#logrecord-attributes
formatter = logging.Formatter(
fmt='\n'.join([
'[%(name)s] %(asctime)s.%(msecs)d',
'\t%(pathname)s [line: %(lineno)d]',
'\t%(processName)s[%(process)d] => %(threadName)s[%(thread)d] => %(module)s.%(filename)s:%(funcName)s()',
'\t%(levelname)s: %(message)s\n'
]),
datefmt='%Y-%m-%d %H:%M:%S'
)
# stream_handler = logging.StreamHandler()
# stream_handler.setFormatter(formatter)
# logger.addHandler(stream_handler)
file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
return logger