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

Question: How to include default / reserved attributes in the log message? #4

Closed
meitham opened this issue Apr 20, 2022 · 3 comments
Closed

Comments

@meitham
Copy link

meitham commented Apr 20, 2022

Thank you for making this library available. I have been looking at the tests and docs but could not find out how I can include some of the keys you defined as "RESERVED" in my message, such as asctime. I would like to say::

>>> logger.info("hello")
"asctime=2022-04-20 msg='hello'"

without having to pass asctime in as an extra. I have tried the below in my dictconfig but did not work::

        'logfmt': {
            'format': 'on=%(asctime)s level=%(levelname)s module=%(name)s msg=%(message)s',
            'class': 'logfmter.Logfmter',
        },

Is this possible?

@jteppinette
Copy link
Owner

jteppinette commented Apr 20, 2022

This functionality has been added by the latest 0.0.5 release.

Thank you for using the library and providing this support!

Please consider starring or sponsoring this project to help continue development!


Default Keys

You can request that the formatter always include certain attributes on the
log record by using the keys parameter. If the key you want to include in
your output is represented by a different attribute on the log record, then
you can use the mapping parameter to provide that key/attribute mapping.

Notice, the keys parameter defaults to ["at"] and will be overridden
by any provided keys.

import logging
from logfmter import Logfmter

formatter = Logfmter(keys=["at", "processName"])

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logging.basicConfig(handlers=[handler])

logging.error("hello") # at=ERROR processName=MainProceess msg=hello

Utilizing a mapping to convert the processName attribute to process.

Notice, the mapping parameter defaults to {"at": "levelname"} and will be overridden
by any provided mapping.

import logging
from logfmter import Logfmter

formatter = Logfmter(
    keys=["at", "process"],
    mapping={"at": "levelname", "process": "processName"}
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logging.basicConfig(handlers=[handler])

logging.error("hello") # at=ERROR process=MainProceess msg=hello

Date Formatting

If you request the asctime attribute (directly or through a mapping), then the date format
can be overridden through the datefmt parameter.

import logging
from logfmter import Logfmter

formatter = Logfmter(
    keys=["at", "when"],
    mapping={"at": "levelname", "when": "asctime"},
    datefmt="%Y-%m-%d"
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logging.basicConfig(handlers=[handler])

logging.error("hello") # at=ERROR when=2022-04-20 msg=hello

@jteppinette
Copy link
Owner

@meitham Let me know if this resolves your question, then I will close this issue.

@jteppinette jteppinette changed the title Question: How to include RESERVED log items in the log message Question: How to include default / reserved attributes in the log message? Apr 20, 2022
@meitham
Copy link
Author

meitham commented Apr 21, 2022

@jteppinette Very kind of you to have this feature added at such a short notice, much appreciated.

It does indeed solve my issue, thank you very much.

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

2 participants