Write django log to different.

56 views
Skip to first unread message

tolerious feng

unread,
Nov 9, 2015, 7:14:03 AM11/9/15
to Django users
I want to write django log to different, especially I want to log daily log file, such as 2015-11-09-log.txt  2015-11-10-log.txt, this is my settings.py file:

TODAY_STRING = datetime.datetime.now().strftime("%F")
TODAY_LOG_DIR = os.path.dirname(__file__) + "/log/" + TODAY_STRING
# TODAY_LOG_DIR = "/log/" + TODAY_STRING
print "*******"
print SITE_SRC_ROOT
print TODAY_LOG_DIR
print os.path.join(SITE_SRC_ROOT, TODAY_LOG_DIR, "todaytotal.log")
print os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, "todaytotal.log"),
if os.path.exists(TODAY_LOG_DIR):
pass
else:
os.mkdir(TODAY_LOG_DIR)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s: %(asctime)s %(module)s %(process)d %(thread)d %(message)s %(filename)s:%(funcName)s:%(lineno)d'
}
},
'handlers': {
'total': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log", "total.txt"),
},
'todaytotal': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, "todaytotal.txt"),
},
'dengbin': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, 'dengbin.txt'),
},
'haiyang': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, 'haiyang.txt'),
},
'meibo': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, "meibo.txt"),
},
'xiecheng': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, 'xiecheng.txt'),
},
'tolerious': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, 'tolerious.txt'),
}
},
'loggers': {
'xiecheng': {
'handlers': ['xiecheng', 'todaytotal', 'meibo', 'total'],
'propagate': False,
'level': 'DEBUG',
},
'dengbin': {
'handlers': ['dengbin', 'todaytotal', 'meibo', 'total'],
'propagate': False,
'level': 'DEBUG',
},
'haiyang': {
'handlers': ['haiyang', 'todaytotal', 'meibo', 'total'],
'propagate': False,
'level': 'DEBUG',
},
'tolerious': {
'handlers': ['tolerious', 'todaytotal', 'meibo', 'total'],
'propagate': False,
'level': 'DEBUG',
}
},
'root': {
'handlers': ['total'],
'level': 'DEBUG',
},
}


but this doesn't work, it only log 2015-11-09-log.txt, when today is 2015-11-10, 2015-11-10-log.txt not generated.

Serdar Dalgic

unread,
Nov 9, 2015, 7:58:18 AM11/9/15
to django...@googlegroups.com
Instead of solving this problem on django side, I'd recommend using a tool such as logrotate for this problem http://www.rackspace.com/knowledge_center/article/understanding-logrotate-utility 

You can customise it to your needs, daily, weekly or per size basis. 

One problem with solving it on django side would be, as the django settings are loaded when you restart the server, and in production you may not want to load the server every day, the TODAY_STRING would stay as the first day the settings are loaded. So it's a better practice to solve the logging on server side.

Best.

- Serdar Dalgıç <s...@serdardalgic.org>
FLOSS Developer, Life & Nature Hacker
twitter:
https://twitter.com/serdaroncode
https://twitter.com/serdarintowild

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0ca4d5be-88de-4e2e-afee-1c3b65bc3132%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen J. Butler

unread,
Nov 9, 2015, 12:20:20 PM11/9/15
to django...@googlegroups.com
That's not going to work because all of this stuff...

On Sun, Nov 8, 2015 at 9:40 PM, tolerious feng <toleri...@gmail.com> wrote:
TODAY_STRING = datetime.datetime.now().strftime("%F")
TODAY_LOG_DIR = os.path.dirname(__file__) + "/log/" + TODAY_STRING
# TODAY_LOG_DIR = "/log/" + TODAY_STRING
print "*******"
print SITE_SRC_ROOT
print TODAY_LOG_DIR
print os.path.join(SITE_SRC_ROOT, TODAY_LOG_DIR, "todaytotal.log")
print os.path.join(SITE_SRC_ROOT, "log/" + TODAY_STRING, "todaytotal.log"),
if os.path.exists(TODAY_LOG_DIR):
pass
else:
os.mkdir(TODAY_LOG_DIR)

... is only run once, when you startup the app. TODAY_STRING won't change until the next time you restart Django.

Instead look logging.TimedRotatingFileHandler.

Reply all
Reply to author
Forward
0 new messages