question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Store logger.log() output into variable

See original GitHub issue

Hi. I want to store the output of logger.log("Some logging message") into variable. The reason for that is that I want to send some logging messages into my Email from the program same as they shown in the terminal console for instance

>> log_msg = logging.log("Storing data...")
# Nothing should print into the terminal console
>> print(log_msg)
# Expected output
2021-02-20 23:11:48.955 | INFO     | TeleBot:store_data:69 - Storing data...

# And of course, I would like to send it to my mail
>> send_logging_message_to_mail(log_msg)

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
obviousrebelcommented, Feb 23, 2021

@Delgan Thank you. I have some misunderstanding. logger.add(send_logging_message_to_mail, filter=should_send_mail) As i understand the first parameter is sink or logging Handler object. I probably need custom logging handler that will receive the logging message from loguru and send it to mail. Can you help me with that handler? I tried myself and I didn’t managed to make that.

The first parameter can also be a callable (function) so should work as is, just update to point to your function that takes message as a parameter.

1reaction
Delgancommented, Feb 21, 2021

Thanks @obviousrebel for pointing that out. Indeed, it’s not possible to retrieve the output of a logging call. Actually, there is no such output because it depends entirely of the added handlers and their format configuration. Each handler has its own output.

You should instead add your send_logging_message_to_mail() as a sink and use maybe a filter combined with bind() to decide when to send the email.

def should_send_mail(record):
    return record["extra"].get("send_mail", False)

logger.add(send_logging_message_to_mail, filter=should_send_mail)

logger.info("Basic message")
logger.bind(send_mail=True).info("Storing data and sending mail...")
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python rolling log to a variable - Stack Overflow
Use the technique described in Capturing Python Log Output In A Variable, but capture it into a custom stream that throws away old...
Read more >
Capturing Python Log Output In A Variable - gists · GitHub
Capturing Python Log Output In A Variable. ... logger = logging.getLogger('basic_logger'). logger.setLevel(logging.DEBUG) ... log_capture_string.close().
Read more >
Capturing Python Log Output In A Variable - Alan W. Smith
Capturing Python Log Output In A Variable · Create the logger. logger = logging. · Setup the console handler with a StringIO object...
Read more >
Logger log() Method in Java with Examples - GeeksforGeeks
log (Level level, String msg): This method is used to Log a message, with no arguments.only message will be written in logger Output....
Read more >
AWS Lambda function logging in Java
This page describes how to produce log output from your Lambda function's code, or access logs using the AWS Command Line Interface, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found