Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed changed #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Proposed changed #1

wants to merge 2 commits into from

Conversation

alex-ber
Copy link

@alex-ber alex-ber commented Jun 12, 2021

Hi,

I will be glad to receive permission from you to redistribute my code based on your.

I have come across your repository here https://stackoverflow.com/questions/47325506/making-python-loggers-log-all-stdout-and-stderr-messages. Quote from their: " But be careful to capture stdout because it's very fragile."
I decided to focus on redirecting stderr only to the logger. If you want you can also redirect stdout, by making 2 calls to initStream() package-level method. But, because of https://unix.stackexchange.com/questions/616616/separate-stdout-and-stderr-for-docker-run it is sufficient only to do it for stderr for me (I'm running my application insider Docker container using tty).

Usage example:
logging.config.dictConfig(kwargs[conf.GENERAL_KEY][conf.LOG_KEY]) errLogger = logging.getLogger("stderr") stdlogging.initStream(errLogger)

Configuration file (simplified):

`
general:

 log:
   version: 1
   disable_existing_loggers: False
   formatters:
     brief:
       format: "%(message)s"
     detail:
       format: "%(asctime)s %(levelname)s %(threadName)s [%(name)s.%(funcName)s] %(message)s"
       datefmt: "%Y-%m-%d %H:%M:%S"


handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: detail
    stream: ext://sys.stdout

  all_file_handler:
    class: logging.handlers.TimedRotatingFileHandler
    level: DEBUG
    formatter: detail
    filename: "logs/stream.log"
    when: midnight
    backupCount: 10
    delay: True

  stderr_handler:
    class: logging.handlers.TimedRotatingFileHandler
    level: DEBUG
    formatter: brief
    filename: "logs/stream.log"
    when: midnight
    backupCount: 10
    delay: True

  stderr_console_handler:
    class: logging.StreamHandler
    level: DEBUG
    formatter: brief
    stream: ext://sys.stdout

loggers:
  hiyapyco:
    level: WARNING
  stderr:
    handlers: [stderr_handler, stderr_console_handler]
    propagate: False



root:
  level: DEBUG
  handlers: [all_file_handler,console]

`

The essential part is

stderr: handlers: [stderr_handler, stderr_console_handler] propagate: False

@fx-kirin
Copy link
Owner

fx-kirin commented Jun 13, 2021

Thanks! I'm okay with your proposal. I'll look into it then merge.

@alex-ber
Copy link
Author

Thanks, for permission. I've released your module as part of my AlexBerUtils project, see https://github.com/alex-ber/AlexBerUtils/blob/master/alexber/utils/stdlogging.py
I've written description of it here https://alex-ber.medium.com/stdlogging-module-d5d69ff7103f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants