Python Forum

Full Version: how to create my own custom logging message
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

Could any one please let me know, how to create my own custom logging.

For example below:

2020-05-15 11:55:17,823: [action_done] Python is simply awesome

here -- 2020-05-15-15 11:55:17,823 = %(asctime)s
Python is simply awesome = %(message)s
[action_done] = ?

So I wanted to add this custom message '[action_done]' to each log print while logging. And it should be able to do it in all the method of that corresponding class.

Can any one suggest. Thanks in advance.

Regards,
Maiya
You just pass a regular string to the formatter. But it will parse out your (usually old-style) format variables. Just put in anything else you want though.

import logging

logging.basicConfig(
        format='%(asctime)s: [action_done] %(message)s',
        level=logging.DEBUG
        )
logging.debug("Mymessage")
Output:
2020-05-15 22:18:37,347: [action_done] Mymessage
Thanks sir

How to add multiple levels into it, and it should print both the levels.
For example:
logging.debug('my message')
logging.info('simply awesome')

Cool, I got it. Thanks
Hi Sir,

How can I provide the file size limit and it should start write into a new log file once it reaches the file limit.
Thanks a lot.

Regards,
Maiya
Hi All,
any help regarding this, much appreciated. Thanks a lot.
Regards,
Maiya