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

Customize logging messages produced by Connexion #640

Closed
sknick opened this issue Jul 25, 2018 · 4 comments
Closed

Customize logging messages produced by Connexion #640

sknick opened this issue Jul 25, 2018 · 4 comments

Comments

@sknick
Copy link

sknick commented Jul 25, 2018

I've spent a while trying to customize the format of the logging being done by Connexion to no avail. Specifically, I want to add a timestamp to the front of each message that is logged. I have tried adding a StreamHandler with a Formatter to the root logger which does nothing. I've tried adding a StreamHandler with a Formatter to the "connexion" logger which does nothing. I've tried directly accessing the Flask app's logger as described in another question, but it does nothing. What am I missing?

@woutervh
Copy link

woutervh commented Aug 8, 2018

I put my logging in a yaml-file and load via dictconfig:

# This is a comment.
# Please indent this yaml-file with 4 spaces, just like in python.
# This config-file configures the logging used in python-applications.
#
# For more information, please see:
#   - https://www.python.org/dev/peps/pep-0391/
#   - https://pyyaml.org/wiki/PyYAMLDocumentation
#   - https://docs.python.org/2.7/library/logging.config.html
#   - http://docs.python-guide.org/en/latest/writing/logging
#   - https://docs.python.org/3/howto/logging-cookbook.html
#   - https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python
#   - https://logmatic.io/blog/python-logging-with-json-steroids
#   - https://docs.djangoproject.com/en/1.8/topics/logging/#examples
#
# Available default levels:
#   -  0 NOTSET
#   - 10 DEBUG
#   - 20 INFO
#   - 30 WARNING  (WARN is deprecated)
#   - 40 ERROR
#   - 50 CRITICAL (=FATAL)


version: 1

disable_existing_loggers: True

formatters:

    generic:
        format: "%(asctime)s - %(name)s - %(levelname)-7s - %(message)s"
        datefmt: "%Y/%m/%d %H:%M:%S"

    simple:
        format: "%(asctime)s - %(levelname)-7s - %(message)s"
        datefmt: "%Y/%m/%d %H:%M:%S"

handlers:

    debug_file: &default
        class: logging.handlers.RotatingFileHandler
        level: DEBUG
        formatter: generic
        filename: debug.log
        maxBytes: 104857600  # 100 MB = 100*1024*1024
        backupCount: 10
        encoding: utf8

    connexion_file_handler:
        <<: *default
        filename: connexion.log
        formatter: simple
        level: DEBUG

    connexion_validation_errors_file_handler:
        <<: *default
        filename: connexion-validation-errors.log
        formatter: simple
        level: DEBUG


loggers:

    # Notes:
    # -) A disabled logger does nothing. If your application, or a library it uses underneath,
    #    instantiates a logger, that logger is disabled unless you add it here.
    # -) Propagation goes bottom-up, not top-to-bottom.
    #    propagate defaults to True.
    #    Therefore propagate is not applicable for the root-logger.

    connexion:
        level: DEBUG
        propagate: true
        handlers:
            - connexion_file_handler

    connexion.decorators.validation:  # validation-errors -> good to enable this
        level: DEBUG
        propagate: false
        handlers:
            - connexion_validation_errors_file_handler


root:

    # You can get the root-logger via logging.getLogger(),
    # Normally you don't need to use the root-logger directly, since all other loggers descend from it.

    level: DEBUG
    handlers:
        - debug_file

@dtkav
Copy link
Collaborator

dtkav commented Aug 29, 2018

Thanks @woutervh for the great example. @sknick did that help?

@sknick
Copy link
Author

sknick commented Aug 29, 2018

I haven't had a chance to play with this more yet, but it should. I haven't configured logging with a dict before, so that will be new to me.

@dtkav
Copy link
Collaborator

dtkav commented Sep 20, 2018

Hey @sknick - I'm going to close this issue for now - please re-open if you continue to have problems!

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

No branches or pull requests

3 participants