1

I am using python logging, at one point of program I need a logging format like this,

"[%(asctime)s] [%(service_id)s] [%(
    levelname)s] [%(hostname)s] [%(host_ip)s] [%(client_agent)s] [%(client_ip)s] [%(filename)20s:%(lineno)d]"

at another point I need a format like this and so on,

 "[%(asctime)s] [%(service_id)s] [%(
    levelname)s] [%(hostname)s] [%(host_ip)s]"

My logging format should dynamically change through out the program. How to achieve this?

1
  • You could just call handler.setFormatter(...) everytime you need a new format? Commented May 10, 2021 at 12:39

1 Answer 1

2

you can change the format by calling:

logging.basicConfig(format="%(asctime)s %(service_id)s %(
levelname)s %(hostname)s %(host_ip)s %(message)s")

or

logging.basicConfig(
  format="%(asctime)s {} %(message)s".format(get_your_hostname_somewhere())
)

the above as pointed by blues comment, would work on the initial setup only.

I believe to get hostname(and all the other fancy data) in there like that you will need to implement a logging.Filter or format it into your logged message if they are defined in the context you call your logger.

After a second review I came across this HOWTO which might be helpful as well, a given example would be something along the lines of:

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s %(service_id)s %(
levelname)s %(hostname)s %(host_ip)s %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

logger.info("message")

and on the other file get the logger and define a handler once more, but define the Formatter with the new format.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work. basicConfig can only be used to setup logging once. Calling it again will not change the format (or anything else)
@blues indeed, thank you for mentioning it. edited response to try and make it more clear, also found more documentation that might provide helpful for this case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.