トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

python:django:ログ出力設定 の変更点

Top / python:django:ログ出力設定

*python:django:ログ出力設定 [#vbd4add9]

djangoのログ出力設定です。

基本はsettings.pyの最後にログの設定があるので、そこをいじります。

※settings.pyの該当箇所の前後2行くらいを載せてます。

***ログ出力設定:settings.py [#r7102a56]

まず、django1.4だとformatterの設定が入っていないようなので、以下の通りに追加。


 LOGGING = {
     'version': 1,
     'disable_existing_loggers': False,
     'formatters': {
         'verbose': {
             'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
         },
         'simple': {
             'format': '%(levelname)s %(message)s'
         },
     },
     'filters': {

次に、Handlerの設定を追加します。デフォルトではいているのは「mail_admins」で、追加したHandlerは「to_file」です。名前イケてないですねーw


    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'to_file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'log/application.log',
            'formatter': 'verbose'
        },
    },

最後に、loggerの設定を追加します。追加したloggerは「application」です。

    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'application': {
            'handlers': ['to_file'],
            'level': 'DEBUG',
            'propagate': True,
        },

    }

***出力部分 [#g7fb0bdf]
たとえば、views.pyでこんな感じで書けばログは出力されます。

 import logging
 
 logger = logging.getLogger('application') #先に設定したloggerを指定する。
 
 def index(request):
     logger.info( "LogTEST:OK!" )

まあ、こんな感じです。

***logger [#k1dee6ff]
logger.logで毎回ログレベルを指定して出力することもできるけど、それぞれメソッドが用意されているのでそっちを使ったほうが便利かも

-debug(msg[,*args[, **kwargs]])
-info(msg[, *args[, **kwargs]])
-warning(msg[, *args[, **kwargs]])
-error(	msg[, *args[, **kwargs]])
-critical(msg[, *args[, **kwargs]])
-exception(msg[, *args])

***参考サイト等 [#b9fd2f6f]

■python2.5のログのところ(日本語)
-http://www.python.jp/doc/2.5/lib/module-logging.html

■django1.4のログのところ(英語)
-https://docs.djangoproject.com/en/1.4/topics/logging/