yaml로 설정
import logging.config, os, yaml
loggingConfigPath = 'logging.yaml'
if os.path.exists(loggingConfigPath):
with open(loggingConfigPath, 'rt') as f:
loggingConfig = yaml.load(f.read())
logging.config.dictConfig(loggingConfig)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger(__name__)
logging.debug("debug")
logging.info("info")
logging.yaml
---
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: simple
stream: ext://sys.stdout
logfile:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: simple
filename: ../log/info.log
maxBytes: 10485760 # 10MB
backupCount: 20
root:
level: DEBUG
handlers: [console, logfile]
날짜별 로그파일 생성
위의 yaml에서 파일 핸들러를 다음과 같이 수정한다
logfile:
class: logging.handlers.TimedRotatingFileHandler
level: DEBUG
formatter: simple
when: D
backupCount: 1
filename: ../log/info.log
when 의 종류는 다음과 같다
'S' : Seconds
'M' : Minutes
'H' : Hours
'D' : Days
'W0'-'W6' : Weekday (0=Monday)
'midnight' : Roll over at midnight

