logging shiny2

Published onesixx on

Root logger에 모든 handlers filters들 다 때려넣어

하지만, root logger를 직접적으로 Root logger는 사용하지 마라

logging.info(“xxx”) ==> logger=logging.getLogger(“__name__”) ; logger.info(“xxx”)

하나의 주요 component마다 하나의 logger사용

store config in JSON or YAML

ROOT loggers / filters/ formatters/ handlers

https://docs.python.org/3/library/logging.html#logrecord-attributes

log_cfg.json

{
    "version": 1,
    "disable_existing_loggers": false,
    "filters": {},
    "formatters": {
        "simple": {
            "format": "%(asctime)s | %(filename)s:L%(lineno)d | %(levelname)s | %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S"
        },
        "verbose": {
            "format": " [%(asctime)s | %(levelname)s | %(module)s | L%(lineno)d ] %(message)s",
            "datefmt": "%Y-%m-%dT%H:%M:%S%z"
        }
    },
    "handlers": {
        "stdout": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        },
        "file": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "WARNING",
            "formatter": "verbose",
            "filename": "template/multipage/logs/xxx_app.log",
            "maxBytes": 10240,
            "backupCount": 16,
            "encoding": "utf8"
        }
    },
    "loggers": {
        "root": {
            "level": "DEBUG",
            "handlers": ["stdout", "file"]
        }
    }
}

log_set.py

import logging
from shared import ASSET_DIR, LOG_DIR
import json
import atexit
# print(f">>>>>>{LOG_DIR}")
logger = logging.getLogger("my_app")

def setup_logging():
    config_file = ASSET_DIR.joinpath("log_cfg.json")
    with open(config_file) as f_in:
        config = json.load(f_in)
    logging.config.dictConfig(config)

app.py

from shiny import App, Inputs, Outputs, Session, ui
from shared import WWW_DIR
from assets.log_set import logger, setup_logging
setup_logging()

from basic_ui import bas_ui
from basic_server import bas_server

logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("\n error message")
try:
    1 / 0
except ZeroDivisionError:
    logger.exception("exception message")
logger.critical("critical message")

ui

from assets.log_set import logger
logger.info("ui start"),

server

from assets.log_set import logger 
logger.info("server start")

old loggingQueue Handler 사용하여, 성능향상

Queue Handler for Non-blocking Logging

json line 포멧으로 log남기기

Parsabel

Categories: Uncategorized

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x