I have an app named main
in my Django project. This app has a several management commands that I want to log, but nothing is showing up in stdout with the following configuration:
我的Django项目中有一个名为main的应用程序。这个应用程序有几个我想记录的管理命令,但是没有任何东西出现在stdout中,具有以下配置:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'log_to_stdout': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
},
},
'loggers': {
'main': {
'handlers': ['log_to_stdout'],
'level': 'DEBUG',
'propagate': True,
}
}
}
What am I doing wrong? I've tried using my_project.main
as well, but that didn't work either. I'm using 1.3.0 final.
我究竟做错了什么?我也尝试过使用my_project.main,但这也不起作用。我正在使用1.3.0决赛。
3 个解决方案
#1
13
you need to namespace your logger. currently you are logging to the root logger, which isn't caught by your handler, which is looking for main
您需要命名您的记录器。目前您正在登录到根记录器,该记录器未被您的处理程序捕获,该处理程序正在寻找main
rather than logging.debug("message")
, you want
而不是logging.debug(“message”),你想要的
logger = logging.getLogger('main')
logger.debug("message")
#2
2
Setting "stream" to sys.stdout is not necessary. However, you should define a formatter:
不需要将“stream”设置为sys.stdout。但是,您应该定义格式化程序:
Example:
例:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'log_to_stdout': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'main': {
'handlers': ['log_to_stdout'],
'level': 'DEBUG',
'propagate': True,
}
}
}
#3
-2
If you are enrolling these custom commands with cron, you can "force" logging by just redirecting output to a text file.
如果您使用cron注册这些自定义命令,则只需将输出重定向到文本文件即可“强制”记录。
* 12 * * * python /path/to/my/custom/command.py >> /path/to/my/logfile.txt
This will run a cron job at 12am every morning, and anything directed to stdout (like python print statements) will be dumped into this text file, concatenated on any existing text in the logfile.
这将在每天早上12点运行一个cron作业,任何指向stdout的内容(如python print语句)都将被转储到此文本文件中,并连接到日志文件中的任何现有文本。
If you're not using cron, just create a single shell script that runs all the scripts you need to run and manually direct them to the logfiles you want.
如果您不使用cron,只需创建一个shell脚本,该脚本运行您需要运行的所有脚本并手动将它们定向到您想要的日志文件。
python /path/to/my/custom/command.py >> /path/to/my/logfile.txt
...and so on.
...等等。
#1
13
you need to namespace your logger. currently you are logging to the root logger, which isn't caught by your handler, which is looking for main
您需要命名您的记录器。目前您正在登录到根记录器,该记录器未被您的处理程序捕获,该处理程序正在寻找main
rather than logging.debug("message")
, you want
而不是logging.debug(“message”),你想要的
logger = logging.getLogger('main')
logger.debug("message")
#2
2
Setting "stream" to sys.stdout is not necessary. However, you should define a formatter:
不需要将“stream”设置为sys.stdout。但是,您应该定义格式化程序:
Example:
例:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'log_to_stdout': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'main': {
'handlers': ['log_to_stdout'],
'level': 'DEBUG',
'propagate': True,
}
}
}
#3
-2
If you are enrolling these custom commands with cron, you can "force" logging by just redirecting output to a text file.
如果您使用cron注册这些自定义命令,则只需将输出重定向到文本文件即可“强制”记录。
* 12 * * * python /path/to/my/custom/command.py >> /path/to/my/logfile.txt
This will run a cron job at 12am every morning, and anything directed to stdout (like python print statements) will be dumped into this text file, concatenated on any existing text in the logfile.
这将在每天早上12点运行一个cron作业,任何指向stdout的内容(如python print语句)都将被转储到此文本文件中,并连接到日志文件中的任何现有文本。
If you're not using cron, just create a single shell script that runs all the scripts you need to run and manually direct them to the logfiles you want.
如果您不使用cron,只需创建一个shell脚本,该脚本运行您需要运行的所有脚本并手动将它们定向到您想要的日志文件。
python /path/to/my/custom/command.py >> /path/to/my/logfile.txt
...and so on.
...等等。