Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:50:36+00:00 2026-05-30T08:50:36+00:00

The script below is writing all errors to both log file and console, except

  • 0

The script below is writing all errors to both log file and console, except for the raised exception which is only written on console and not on log. How to I get it to write the raised exception to log, or any runtime exception for that matter? Thanks.

import os
import sys
import logging
import logging.config

class Main(object):

    @staticmethod
    def main():
    logging.config.fileConfig("logging.conf")
    logging.debug("1")
    logging.info("2")
    logging.warn("3")
    logging.error("4")
    logging.critical("5")
    raise Exception("test")

if __name__ == "__main__":
    Main.main()


import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
raise Exception("Exception raised")

Config file:

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_fileHandler]
formatter=simpleFormatter
args=('error.log')

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:50:37+00:00Added an answer on May 30, 2026 at 8:50 am

    In order to have all the errors captured using the logging module, the first requirement is for you to catch them all using except statements. Once you catch them, you then have to call Logger.exception() or another suitable function according to the level of error.

    If you cannot catch all exceptions beforehand, your best bet would be to redirect stdout and stderr to a file. Then, do a tail -f to simulate a console output. Anyway, one uncaught exception will result in your program execution being stopped.

    But, I would prefer trying to catch all exceptions even if that means having to do something like this.

    try:
        Main.main()
    except Exception as e:
        logging.Exception("Unexpected exception! %s",e)
    

    This allows you to use the neat logging module instead of having to rely on crappy output redirection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The script below will replace selected word in a textarea. But it only works
The script below resides in my theme's functions.php file. It is designed to show
The script below should open all the files inside the folder 'pruebaba' recursively but
I have tested the below script on a demo page which is not using
I have written an SQL script having the below query. The query works fine.
I'm writing a script to search for a pattern in file. For example scriptname
[Edit 2: More information and debugging in answer below...] I'm writing a python script
All, Below is the code that I have written in JS. <html> <head> <title>Fibonacci
The script below, test.php, is intended to be placed in a specific directory of
The script below worked on my Mac OS X. I'm now using Ubuntu OS,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.