Last active
August 28, 2018 08:29
-
-
Save syfun/4c1ede8fab1793a79dbb9ad9dc066818 to your computer and use it in GitHub Desktop.
Python RotatingFileHandler and StreamHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask.globals import _find_app, LocalProxy, partial | |
def _find_app_attr(name): | |
app = _find_app() | |
return getattr(app, name) | |
""" | |
This can be only used in app context. | |
app config need LOGGER_HANDLER_POLICY. | |
dev config: debug | |
production config: production | |
Use like this: | |
from app.logging import logger | |
@app.route('/some'): | |
def some(): | |
logger.info() | |
""" | |
logger = LocalProxy(partial(_find_app_attr, 'logger')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
""" | |
You can use this module to handler log. | |
Example: | |
from app import log as logging | |
LOG = logging.getLogger(__name__) | |
LOG.debug('Hello') | |
LOG.info('Hello') | |
LOG.warning('hello') | |
LOG.error('hello') | |
LOG.critical('hello') | |
""" | |
import logging | |
import os | |
from logging import config, LoggerAdapter | |
import six | |
import yaml | |
base_dir = os.path.abspath(os.path.dirname(__file__)) | |
LOG_CONF = os.path.join(base_dir, '../logging.yaml') | |
_loggers = {} | |
with open(LOG_CONF, 'rb') as f: | |
conf = yaml.load(f) | |
config.dictConfig(conf) | |
class ContextAdapter(LoggerAdapter): | |
def __init__(self, logger, name): | |
self.logger = logger | |
self.name = name | |
def process(self, msg, kwargs): | |
# If msg is not unicode, coerce it into unicode. | |
if not isinstance(msg, six.text_type): | |
msg = six.text_type(msg) | |
return msg, kwargs | |
def getLogger(name='unknown'): | |
if name not in _loggers: | |
_loggers[name] = ContextAdapter(logging.getLogger(name), name) | |
return _loggers[name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
version: 1 | |
disable_exsiting_loggers: True | |
formatters: | |
default: | |
format: '[%(asctime)s] -- %(levelname)s %(name)s %(pathname)s:[%(funcName)s]:(at %(lineno)d) -- %(message)s' | |
handlers: | |
console: | |
level: INFO | |
class: logging.StreamHandler | |
formatter: default | |
file: | |
level: INFO | |
class: logging.handlers.RotatingFileHandler | |
formatter: default | |
filename: plm.log | |
maxBytes: 1024 | |
backupCount: 3 | |
root: | |
handlers: [console, file] | |
level: INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import logging | |
from logging.handlers import RotatingFileHandler | |
def getLogger(name): | |
logger = logging.getLogger(name) | |
logger.setLevel('INFO') | |
formatter = logging.Formatter( | |
'%(asctime)s %(levelname)s %(name)s %(message)s ' | |
'[in %(pathname)s:%(lineno)d]') | |
# RotatingFileHandler | |
logger_handler = RotatingFileHandler('{}.log'.format(name), maxBytes=10*1024*1024, backupCount=2) | |
logger_handler.setLevel('INFO') | |
logger_handler.setFormatter(formatter) | |
# StreamHandler | |
stream_handler = logging.StreamHandler() | |
stream_handler.setFormatter(formatter) | |
logger.addHandler(logger_handler) | |
logger.addHandler(stream_handler) | |
return logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment