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.

Terminate on ERROR level

See original GitHub issue

Hi,

I’m looking to log different levels for my application such as INFO and ERROR.

When an ERROR is logged, I would like the application to terminate. Is there a way that loguru can handle this for me instead of checking the level manually and calling exit()?

Many Thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Apr 13, 2021

Meh. I overlooked the sink accept a message string and not the record directly, so it must be slightly modified.

You can also use two sinks: the first one will check the level and exit your application if needed. The other one will simply log the messages to your file unconditionally.

def exit_on_error(message):
     if message.record["level"].name == "ERROR":
        exit()

logger.add(exit_non_error, level="ERROR")
logger.add("app.log")

But you can also use the filter solution just as before, make sure to return True so logs are not discarded.

def check_log_record_level(record):
    if record["level"].name == "ERROR":
        exit()
    return True

logger.add("app.log", filter=check_log_record_level)
1reaction
dbrennandcommented, Apr 13, 2021

Awesome! Thanks a lot @Delgan

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exit - Terminate a script - Windows CMD - SS64.com
An errorlevel of -1073741510 will be interpreted by CMD.exe as a Ctrl-C Key sequence to cancel the current operation, not the entire script...
Read more >
What are batch file exit codes or errorlevels? - ManageEngine
Use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file.
Read more >
Control Error Level for cob Termination (-W err-level)
Causes cob processing to terminate after the COBOL Compiler err-level has been exceeded. By default, the cob command terminates if your code contains ......
Read more >
Windows batch exit option b with or without errorlevel
If an environment variable named ERRORLEVEL is defined then %errorlevel% always resolves to its value (which can be arbitrary), and not to the ......
Read more >
Common program exit codes and error levels - Febooti, Ltd.
Incorrect function. Indicates that Action has attempted to execute non-recognized command in Windows command prompt cmd.exe . Code 2. 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