gpt4 book ai didi

python - 将错误记录到两个不同的文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:54 24 4
gpt4 key购买 nike

我是 python 上的 logging 模块的新手,我决定在我的一个程序中使用它。这是我的功能

def _get_local_err_logger():

try:
# logs_dir = "logs"
parent_dir = exec_dir
logs_dir = os.path.join(parent_dir, "logs")
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
except Exception as e:
print str(e)
exit(1)

logging.basicConfig(
filename=os.path.join(logs_dir, 'cli.log'),
level='ERROR',
format='%(asctime)s %(levelname)s: %(message)s'
)
return logging.getLogger('cli.log')

在 main()..

exec_file = os.path.abspath( argv[0] )
exec_dir = os.path.dirname( exec_file )

try:
log_filename = args.logfile if args.logfile else config.get('Logging', 'filename')
except (NoOptionError, NoSectionError) as e:
logger = _get_local_err_logger()
logger.error( str( e ) )
exit(1)

if not os.path.isabs( log_filename ):
log_filename = os.path.join(exec_dir, log_filename)

try:
logging.basicConfig(
filename=log_filename,
level=config.get( 'Logging', 'log_level' ),
format=config.get( 'Logging', 'format' )
)
logger = logging.getLogger('cli.log')
except Exception as e:
logger = _get_local_err_logger()
logger.exception( "Logging configuration error({0})".format( str( e ) ) )
exit(1)

但是我刚刚注意到一个奇怪的问题(主要是因为我可能没有完全理解 logging 模块)。我从配置文件中读取了一些凭据( key )以及以下内容:

[Logging]
filename = cli.log
log_level = INFO
format = %(asctime)s %(levelname)s: %(message)s

当我使用我的程序(CLI)时,所有日志都写入与程序位于同一目录中的名为 cli.log 的文件中。但是,当我注释掉配置文件中的 [Logging] 部分时,错误 (NoSectionError) 被写入名为 'cli.log' 的文件夹中的文件中在我的程序所在的目录中创建日志。我希望我所有的日志都进入 logs 目录中的文件,但我不确定为什么要写入 2 个不同的文件。

如果我将 main() 中的代码更改为:

try:
log_filename = args.logfile if args.logfile else config.get('Logging', 'filename')
except (NoOptionError, NoSectionError) as e:
logger = _get_local_err_logger()
logger.error( repr( e ) )
exit(1)

if not os.path.isabs( log_filename ):
logs_directory = os.path.join( exec_dir, "logs" )
if not os.path.exists( logs_directory ):
os.makedirs( logs_directory )
log_filename = os.path.join( logs_directory, log_filename )

try:
logging.basicConfig(
filename=log_filename,
level=config.get( 'Logging', 'log_level' ),
format=config.get( 'Logging', 'format' )
)
logger = logging.getLogger('cli.log')
except Exception as e:
logger = _get_local_err_logger()
logger.exception( "Logging configuration error({0})".format( str( e ) ) )
exit(1)

然而,这几乎是重复函数中的代码,并且违反了 DRY。有人可以指出错误可能是什么吗?

最佳答案

def _get_logger(pdir,ldir,lname,level,fmt):
try:
logs_dir = os.path.join(pdir, ldir)
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
except Exception as e:
print e
exit(1)

logging.basicConfig(
filename=os.path.join(logs_dir, lname),
level=level,
format=fmt
)
return logging.getLogger('cli.log')


exec_file = os.path.abspath(argv[0])
exec_dir = os.path.dirname(exec_file)
#build a dict with default configuration values for the logger
default_logger = dict(pdir=exec_dir,ldir='logs',lname='cli.log',level='ERROR',
fmt='%(asctime)s %(levelname)s: %(message)s')

try:
log_filename = args.logfile if args.logfile else config.get('Logging', 'filename')
level = config.get('Logging', 'log_level')
format = config.get('Logging', 'format')
except (NoOptionError, NoSectionError) as e:
#unpack the configuration dict and call _get_logger
logger = _get_logger(**default_logger)
logger.error(repr(e))
exit(1)
else:
# we have successfully read configuration from file, so update
# configuration dict to reflect new settings
default_logger.update(fmt=format,level=level,lname=log_filename)
if os.path.isabs(log_filename):
# log_filename is an absolute one. split it to get filename and dirname
# and update configuration dict
bdir, log_filename = os.path.split(log_filename)
default_logger.update(pdir='',ldir=bdir,lname=log_filename)
logger = _get_logger(**default_logger)

关于python - 将错误记录到两个不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com