Py_logging
7732 단어 도구 학습
0x1.logging.basicConfig(주의#)
0x1.1 콘솔로 내보내기
import logging
logging.basicConfig(
level = logging.DEBUG, # INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
#,filename='myapp.log',
#filemode='a+'
)
0x1.2 파일로 내보내기
logging.basicConfig(
level = logging.DEBUG, # INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
,filename='myapp.log',
filemode='a+'
)
0x1.3은 콘솔과 파일에 동시에 출력됩니다.
*: root, .
logging.basicConfig(
level = logging.DEBUG, # INFO
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
#,filename='myapp.log',
#filemode='a+'
)
# log . logging , addHandler 。
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
#
file_handler= logging.FileHandler('myapp.log','a+') #FileHandler(filename, mode=’a’, encoding=None, delay=False)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
logging.getLogger('').addHandler(file_handler)
## console
#console_handler = logging.StreamHandler(sys.stdout)
#console_handler.setLevel(logging.DEBUG)
#console_handler.formatter = formatter
#logging.getLogger('').addHandler(console_handler)
0x2. logconfig.
0x2.1 호출
import logging
import logging.config----
#-------------------- ----------
logfilePath = os.path.join(os.path.dirname(__file__), 'log\\logging.conf')
logging.config.fileConfig('log\\logging.conf')
logging.getLogger()
0x2.2 logging.conf(경로 자체 설정)
# logging.conf
[loggers]
keys=root
[logger_root]
level=DEBUG
handlers=consoleHandler,FileHandler ,errorTimedRotateFileHandler
#,timedRotateFileHandler
#################################################
[handlers]
keys=consoleHandler,timedRotateFileHandler,errorTimedRotateFileHandler,FileHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_timedRotateFileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('log\\debug.log', 'H')
[handler_errorTimedRotateFileHandler]
class=handlers.TimedRotatingFileHandler
level=WARN
formatter= multiLineFormatter
args=('log\\error.log', 'H')
[handler_FileHandler]
class=FileHandler
level=DEBUG
formatter = selfFormatter
args=('log\\myapp.log','a+')
#################################################
[formatters]
keys=simpleFormatter, multiLineFormatter,selfFormatter
[formatter_simpleFormatter]
format= %(levelname)s %(threadName)s %(asctime)s: %(message)s
datefmt=%H:%M:%S
[formatter_multiLineFormatter]
format= ------------------------- %(levelname)s -------------------------
Time: %(asctime)s
Thread: %(threadName)s
File: %(filename)s(line %(lineno)d)
Message:
%(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_selfFormatter]
format = %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
0x3 레코드 예외 - 스택 정보
#
try:
1 / 0
except:
# error ,
logger.exception('this is an exception message')
------------------------- ERROR -------------------------
Time: 2017-09-17 11:04:02
Thread: MainThread
File: ObfuscateEng.py(line 152)
Message:
Eng has a exception info.
Traceback (most recent call last):
File "F:\AlreadyDoneForRecord\test\ObfuscateEng\oldVer\v0.94\ObfuscateEng.py", line 147, in Eng
1/0
ZeroDivisionError: division by zero
0x3 참조
0x4 확장
python에서 누가 자신을 호출했는지 어떻게 알아요?