logger = logging.basicConfig(filename='xxxxxxx.txt', format='%(asctime)s - %(name)s - (levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=30) # 參數設置 # 等級 # logging.debug('x1') # 10 # logging.info('x2') # 20 # logging.warning('x3') # 30 # logging.error('x4') # 40 # logging.critical('x5') # 50 # logging.log(10,'x6')
# 自定義日誌
logging.error('x4')
將日誌寫入一個文件中html
import logging import traceback logger = logging.basicConfig(filename='xxxxxxx.txt', format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=30) def func(): try: a = a +1 except Exception as e: # 獲取當前錯誤的堆棧信息 msg = traceback.format_exc() logging.error(msg) func()
將日誌寫入多個文件中python
import logging # 建立一個操做日誌的對象logger(依賴FileHandler) file_handler = logging.FileHandler('log1.log', 'a', encoding='utf-8') file_handler.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")) logger1 = logging.Logger('s1', level=logging.ERROR) logger1.addHandler(file_handler) logger1.error('1') # 在建立一個操做日誌的對象logger(依賴FileHandler) file_handler2 = logging.FileHandler('log2.log', 'a', encoding='utf-8') file_handler2.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s")) logger2 = logging.Logger('s2', level=logging.ERROR) logger2.addHandler(file_handler2) logger2.error('2')
LOGGING = {
'version': 1, 'disable_existing_loggers': False, 'formatters': FORMATTERS, 'handlers': HANDLERS, 'loggers': { 'django': { 'handlers': ['django', 'mail_admin', 'exception'], 'level': 'INFO', 'propagate': False }, 'django.db.backends': { 'handlers': ['default'], 'level': 'DEBUG', 'propagate': False }, 'access': { 'handlers': ['access'], 'level': 'INFO', 'propagate': False }, 'celery.tasks': { 'handlers': ['mail_admin'], 'level': 'INFO', 'propagate': False }, 'parso': { 'handlers': ['parso'], 'level': 'INFO' } }, 'root': { 'handlers': ['default', 'mail_admin', 'exception'], 'level': 'DEBUG' }, }
disable_existing_loggers若是設置其值爲true,全部日誌的默認設置都將被禁用,因此謹慎設置django
FORMATTERS = {
'access': { 'format': '%(client_ip)s %(x_forwarded_ip)s %(asctime)s %(process)d/%(thread)d %(http_user_agent)s ' '%(server_name)s %(protocol)s %(path)s %(status)s %(content_length)s %(duration)s ' '%(levelname)s %(user)s %(last_login_role)s %(data_b)s %(message)s', 'datefmt': "%Y/%m/%d %H:%M:%S" }, 'django': { 'format': '[%(asctime)s] %(process)d/%(thread)d %(levelname)s %(message)s', 'datefmt': "%Y/%m/%d %H:%M:%S" }, 'exception': { 'format': '[%(asctime)s] %(process)d/%(thread)d %(name)s %(funcName)s %(lineno)s %(levelname)s %(message)s', 'datefmt': "%Y/%m/%d %H:%M:%S" }, 'default': { 'format': '%(asctime)s %(process)d/%(thread)d %(name)s:%(lineno)s %(levelname)s - %(message)s', 'datefmt': "%Y/%m/%d %H:%M:%S" } }
若是datefmt (a string) 被設置, 將使用time.strftime() 來format建立記錄.post
Attribute namethis |
Formatspa |
Descriptiondebug |
---|---|---|
args日誌 |
You shouldn’t need to format this yourself.code |
The tuple of arguments merged into |
asctime |
|
Human-readable time when the |
created |
|
Time when the |
exc_info |
You shouldn’t need to format this yourself. |
Exception tuple (à la |
filename |
|
Filename portion of |
funcName |
|
Name of function containing the logging call. |
levelname |
|
Text logging level for the message ( |
levelno |
|
Numeric logging level for the message ( |
lineno |
|
Source line number where the logging call was issued (if available). |
message |
|
The logged message, computed as |
module |
|
Module (name portion of |
msecs |
|
Millisecond portion of the time when the |
msg |
You shouldn’t need to format this yourself. |
The format string passed in the original logging call. Merged with |
name |
|
Name of the logger used to log the call. |
pathname |
|
Full pathname of the source file where the logging call was issued (if available). |
process |
|
Process ID (if available). |
processName |
|
Process name (if available). |
relativeCreated |
|
Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded. |
stack_info |
You shouldn’t need to format this yourself. |
Stack frame information (where available) from the bottom of the stack in the current thread, up to and including the stack frame of the logging call which resulted in the creation of this record. |
thread |
|
Thread ID (if available). |
threadName |
|
Thread name (if available). |
你也能夠自定義本身項目接口中的fields
例如在django的自定義中間件中設置
class LoggingMiddleware(object):
def __init__(self, get_response): self.get_response = get_response self.logger = logging.getLogger('django') self.logger_access = logging.getLogger('access') def process_response(self, request, response): logging_dict = { 'duration': time() - request.timer, 'client_ip': request.META.get('REMOTE_ADDR'), 'x_forwarded_ip': 'path': request.full_path, 'status': response.status_code, 'user': user_login_id, 'last_login_role': last_login_role, 'http_user_agent': request.META.get('HTTP_USER_AGENT'), 'server_name': request.META.get('SERVER_NAME'), 'content_length': request.META.get('CONTENT_LENGTH'), 'protocol': request.META.get('SERVER_PROTOCOL'), 'data_b': data_b } self.logger_access.info(data_c, extra=logging_dict) # 經過關鍵字參數extra
HANDLERS = {
'mail_admin': {
'level': 'ERROR',
'class': 'SendEmailHandler'
},
'django': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/logs/server.log',
'when': 'midnight', # 時間後綴
'interval': 1,
'formatter': 'django'
},
'exception': {
'level': 'WARNING',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/logs/exception.log',
'when': 'midnight',
'interval': 1,
'formatter': 'exception'
},
'access': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/logs/access.log',
'when': 'midnight',
'interval': 1,
'formatter': 'access'
},
'parso': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'default'
},
'default': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/logs/default.log',
'when': 'midnight',
'interval': 1,
'formatter': 'default'
}
}
經過自定email,在異常發生時發送郵件
class SendEmailHandler(Handler):
"""An exception log handler that emails log entries to settings.EXCEPTION_MAIL_LIST.
""" actitve_count = 0 @postpone def send_admin_mail(self, subject, message, mail_from, mail_to, fail_silently): if SendEmailHandler.actitve_count < 5: SendEmailHandler.actitve_count += 1 send_mail(subject, message, mail_from, mail_to, fail_silently=fail_silently) SendEmailHandler.actitve_count -= 1 else: pass def emit(self, record): if os.getenv('DONT_SEND_EXCEPTION_MAIL'): # 應用於本地開發 return
subject = '%s: %s %s' % ( ENV.upper(), record.levelname, record.getMessage() ) def format_subject(subject): """ Escape CR and LF characters. """ return subject.replace('\n', ' ').replace('\r', ' ') subject = format_subject(subject) # 郵件標題 message = record.getMessage() + '\n' + traceback.format_exc() self.send_admin_mail(subject, message, EXCEPTION_MAIL_FROM, EXCEPTION_MAIL_LIST, False)
settings中郵件配置
EXCEPTION_MAIL_FROM = ''
EMAIL_HOST = 'smtp.xx.com' EMAIL_PORT = '' EMAIL_HOST_USER = '' EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = True EXCEPTION_MAIL_LIST = []