Daily Develope

[Python] 로그, 로깅 (logging) 본문

Develope/Python

[Python] 로그, 로깅 (logging)

noggame 2022. 1. 15. 10:01
기본 선언 및 환경설정

 

ㅇ 선언

 

import logging
logging.basicConfig(filename=f'{os.getcwd()}/result_test.log', level=logging.INFO, format='%(asctime)s %(message)s')

# 사용 예)
import os
import logging
from datetime import datetime

logging.basicConfig(filename=f'{os.getcwd()}/logs/my_log_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log',
                    level=logging.INFO,
                    format='%(asctime)s %(message)s')

 

 

ㅇ 환경설정 상세 설명

# Fromat

: 로깅의 출력으로 사용 가능한 format 속성 설명

 

# Level

: 로깅 목적에 따라 Level을 구분해 사용하는 것이 좋으며, 각 Level별 사용은 아래 참조

더보기

# Leveling

 

# Level 사용 방법

 

# Level Numeric 값

 


ㅇ 사용 예1) logger 환경설정 및 info level에서의 등록

 

# main.py ---
import os
import logging
from datetime import datetime
import testLogging as tl

logging.basicConfig(filename=f'{os.getcwd()}/logs/my_log_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log',
                    level=logging.INFO,
                    format='%(asctime)s %(message)s')
tl.testLogging()                    
                                        
# testLogging.py ---
import logging

def testLogging():
	logging.info('this is a my log')

 


 

ㅇ 사용 예2) 신규 level 등록

 

# create new level with numeric value 15
logging.addLevelName(15, "DATA")
...
# write log with 'DATA' level (using numeric value)
logging.log(15, "hello")

 

ㅇ 참조

https://docs.python.org/3/library/logging.html

 

Comments