logging shiny

Published by onesixx on

https://docs.python.org/3/library/logging.html

https://www.youtube.com/watch?v=9L77QExPmI0

https://onesixx.com/logging/

app.py

import logging

formatter_s = logging.Formatter('!!!%(asctime)s|%(levelname)s|%(message)s')
formatter_c = logging.Formatter('%(asctime)s|%(levelname)s|%(message)s')

logger = logging.getLogger("shinylog")

handler_s = logging.StreamHandler()
logger.setLevel(logging.DEBUG)
handler_s.setFormatter(formatter_s)
logger.addHandler(handler_s)

handler_c = logging.FileHandler(ASSET_DIR.joinpath("log_event.log"))
handler_c.setLevel(logging.CRITICAL)
handler_c.setFormatter(formatter_c)
logger.addHandler(handler_c)


# logger
logger.info("Is working well?")
logger.critical("AAAAAAAAAAA")

server.py

import logging
logger = logging.getLogger('shinylog')
logger.info(f"bropress endend {p} loading \n {temp_dr}")
logger.critical("bropress start")

기본 Logger

# assets/log_set.py
import logging

def get_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)  # 로그 레벨 설정

    # 콘솔 핸들러 설정
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    # 로그 포맷 설정
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)

    # 핸들러를 로거에 추가
    logger.addHandler(ch)

    return logger

app.py

# app.py
from assets.log_set import get_logger
logger = get_logger("my_app")  
# logger.info("app start")

logger.debug("debug message", extra={"x": "hello"})
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
logger.critical("critical message")
try:
    1 / 0
except ZeroDivisionError:
    logger.exception("exception message")

server.py

# basic_server.py
from assets.log_set import get_logger
logger = get_logger(__name__)
logger.info("server start")

import logging

def get_logger(name):
    logger = logging.getLogger(name)
    logging_config = {
        "level": logging.DEBUG,
        "format": "%(asctime)s | %(name)s | %(levelname)s | %(message)s",
        "datefmt": "%Y-%m-%d %H:%M:%S",
    }
    logging.basicConfig(**logging_config)

    return logger

dictConfig를 통해 config

import logging

def get_logger(name):
    logger = logging.getLogger(name)
    logging_config = {
        "level": logging.DEBUG,
        "format": "%(asctime)s | %(name)s | %(levelname)s | %(message)s",
        "datefmt": "%Y-%m-%d %H:%M:%S",
    }
    logging.basicConfig(**logging_config)

    return logger
import logging
from shared import LOG_DIR
# print(f">>>>>>{LOG_DIR}")

logging_config = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {},
    "formatters": {
        "simple": {
            "format": "%(asctime)s | %(name)s | %(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": 10485760,
            "backupCount": 20,
            "encoding": "utf8"
        }
    },
    "loggers": {
        "root": {
            "level": "DEBUG",
            "handlers": ["stdout", "file"]
        }
    }
}
def get_logger(name):
    logger = logging.getLogger(name)
    logging.config.dictConfig(config=logging_config)
    return logger

Categories: shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

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