質問編集履歴

1

ロガーの設定を追記

2018/02/16 00:44

投稿

limitman80
limitman80

スコア40

test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,85 @@
43
43
 
44
44
 
45
45
  宜しくお願いします。
46
+
47
+
48
+
49
+ ロガーの設定は以下の通りです。
50
+
51
+ ```Python
52
+
53
+ import os
54
+
55
+ from logging import Formatter, DEBUG, INFO, ERROR
56
+
57
+ from logging.handlers import RotatingFileHandler
58
+
59
+
60
+
61
+
62
+
63
+ class AppLogger:
64
+
65
+ maxBytes = 100000
66
+
67
+ backupCount = 10
68
+
69
+
70
+
71
+ # ログ初期処理
72
+
73
+ def init(self, app, debugFilePath='../log/xxxdebug.log'):
74
+
75
+ debugFileFullPath = os.path.join(app.root_path, debugFilePath)
76
+
77
+ # ログディレクトリ作成
78
+
79
+ self.makedir(os.path.dirname(debugFileFullPath))
80
+
81
+
82
+
83
+ # 容量によりローテーションするハンドラ
84
+
85
+ debugFileHandler = RotatingFileHandler(debugFileFullPath, self.maxBytes, self.backupCount)
86
+
87
+
88
+
89
+ # ハンドラのログレベル設定
90
+
91
+ debugFileHandler.setLevel(DEBUG)
92
+
93
+ # ログフォーマット設定
94
+
95
+ debugFileHandler.setFormatter(self.getFormatter())
96
+
97
+ # ログファイル設定ハンドラ追加
98
+
99
+ app.logger.addHandler(debugFileHandler)
100
+
101
+
102
+
103
+ # ログレベル設定
104
+
105
+ app.logger.setLevel(DEBUG)
106
+
107
+
108
+
109
+ # ディレクトリ作成
110
+
111
+ def makedir(self, path):
112
+
113
+ # 存在しなければ、ディレクトリ作成
114
+
115
+ if not os.path.exists(path):
116
+
117
+ os.makedirs(path)
118
+
119
+
120
+
121
+ # ログフォーマット取得
122
+
123
+ def getFormatter(self):
124
+
125
+ return Formatter('%(asctime)s %(filename)s:%(lineno)d %(levelname)s: <%(funcName)s> %(message)s')
126
+
127
+ ```