# log.py
import logging
logging.basicConfig(
filename="log.log",
format="%(asctime)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG,
)
logging.debug("debug message")
logging.info("info message")
logging.warning("warn message")
logging.error("error message")
logging.critical("critical message")
# log.py
import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
info_handler = logging.handlers.RotatingFileHandler(
filename="log/info.log",
maxBytes=10485760, # 10MB = 1024 * 1024 * 10
backupCount=10,
)
error_handler = logging.handlers.RotatingFileHandler(
filename="log/error.log",
maxBytes=10485760, # 10MB = 1024 * 1024 * 10
backupCount=10,
)
info_handler.setLevel(logging.INFO)
error_handler.setLevel(logging.ERROR)
info_handler.addFilter(lambda record: record.levelno == logging.INFO)
error_handler.addFilter(lambda record: record.levelno == logging.ERROR)
formatter = logging.Formatter("%(asctime)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s", "%Y-%m-%d %H:%M:%S")
info_handler.setFormatter(formatter)
error_handler.setFormatter(formatter)
logger.addHandler(info_handler)
logger.addHandler(error_handler)
logger.debug("debug message")
logger.info("info message")
logger.warning("warn message")
logger.error("error message")
logger.critical("critical message")
# log.py
import logging.config
logging.config.fileConfig("logging.conf")
logger = logging.getLogger("logger")
logger.debug("debug message")
logger.info("info message")
logger.warning("warn message")
logger.error("error message")
logger.critical("critical message")
# logging.conf
[loggers]
keys=root,logger
[handlers]
keys=consoleHandler,infoHandler,errorHandler
[formatters]
keys=standardFormatter
[logger_root]
level=NOTSET
handlers=consoleHandler
[logger_logger]
level=DEBUG
handlers=infoHandler,errorHandler
propagate=0
qualname=logger
[handler_consoleHandler]
class=StreamHandler
level=NOTSET
formatter=standardFormatter
args=(sys.stdout,)
[handler_infoHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=standardFormatter
args=("log/info.log", "a", 10485760, 10)
[handler_errorHandler]
class=handlers.RotatingFileHandler
level=ERROR
formatter=standardFormatter
args=("log/error.log", "a", 10485760, 10)
[formatter_standardFormatter]
format=%(asctime)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S
# log.py
import logging.config
import yaml
with open("logging.yaml") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
logging.config.dictConfig(config)
logger = logging.getLogger("logger")
logger.debug("debug message")
logger.info("info message")
logger.warning("warn message")
logger.error("error message")
logger.critical("critical message")
# filters.py
import logging
class DebugFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.DEBUG
class InfoFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.INFO
class WarningFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.WARNING
class ErrorFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.ERROR
class CriticalFilter(logging.Filter):
def filter(self, record):
return record.levelno == logging.CRITICAL
# logging.yaml
version: 1
disable_existing_loggers: True
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
standard:
format: "%(asctime)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
filters:
debug_filter:
(): filters.DebugFilter
info_filter:
(): filters.InfoFilter
warning_filter:
(): filters.WarningFilter
error_filter:
(): filters.ErrorFilter
critical_filter:
(): filters.CriticalFilter
handlers:
console:
class: logging.StreamHandler
level: NOTSET # 0
formatter: standard
stream: ext://sys.stdout
debug_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG # 10
filters: [ debug_filter ]
formatter: standard
filename: log/debug.log
maxBytes: 10485760 # 10MB = 1024 * 1024 * 10
backupCount: 10
encoding: utf-8
info_handler:
class: logging.handlers.RotatingFileHandler
level: INFO # 20
filters: [ info_filter ]
formatter: standard
filename: log/info.log
maxBytes: 10485760 # 10MB = 1024 * 1024 * 10
backupCount: 10
encoding: utf-8
warning_handler:
class: logging.handlers.RotatingFileHandler
level: WARNING # 30
filters: [ warning_filter ]
formatter: standard
filename: log/warning.log
maxBytes: 10485760 # 10MB = 1024 * 1024 * 10
backupCount: 10
encoding: utf-8
error_handler:
class: logging.handlers.RotatingFileHandler
level: ERROR # 40
filters: [ error_filter ]
formatter: standard
filename: log/error.log
maxBytes: 10485760 # 10MB = 1024 * 1024 * 10
backupCount: 10
encoding: utf-8
critical_handler:
class: logging.handlers.RotatingFileHandler
level: CRITICAL # 50
filters: [ critical_filter ]
formatter: standard
filename: log/critical.log
maxBytes: 10485760 # 10MB = 1024 * 1024 * 10
backupCount: 10
encoding: utf-8
root:
level: NOTSET
handlers: [ console ]
loggers:
logger:
level: DEBUG
handlers: [ debug_handler, info_handler, warning_handler, error_handler, critical_handler ]
propagate: no
参考链接
https://docs.python.org/3/library/logging.html
https://gist.github.com/kingspp/9451566a5555fb022215ca2b7b802f19