0

I started using logging in python and I would like to write my logs to file.

The logging module is writing a lot many events to the file. I just want to write my own loggers and do not want to have that default loggers getting logged. How can I avoid that?

Below is the sample logging happening right now:

Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
Changing event name from before-call.apigateway to before-call.api-gateway
Changing event name from request-created.machinelearning. Predict to request-created.machine-learning. Predict Setting config variable for region to 'us-west-2'

The way how I instantiated the logger

logger = logging.getLogger("S3_transfer")

def set_log_output_file(logname):
    if not os.path.exists('logs'):
        os.makedirs('logs')
    logging.basicConfig(filename='logs/{}.log'.format(logname),
                        filemode='a',
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        level=logging.DEBUG)

def get_logger():
    """
    Retrieves the current logger
    :return: Logger
    """
    return logger

Suggestions please?!?!

3
  • 1
    Mabe have a look here: docs.python.org/3/library/logging.html Commented Oct 1, 2019 at 6:13
  • Thanks for your edit, and I did not want to put bullets there for my loggers.. Let those be just log messages and not the highlights with bullet points Commented Oct 1, 2019 at 7:15
  • Actually, logging.INFO level avoided writing default events to the logs. I was wrong thinking that DEBUG comes after INFO. But just confirmed this hierarchy DEBUG < INFO < WARN < ERROR < FATAL Commented Oct 1, 2019 at 16:48

1 Answer 1

0

That happens because multiple calls logging.getLogger() with the same name will always return the same instance of Logger. There is a practice in python packages to create a logger instance by logging.getLogger(__name__), which allows us to get all logs in a single place and configure logger once. But if you don't want to see any logs from external packages just replace __name__ in your file with your own logger name and set basic config which will write to file for it.

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

1 Comment

You may also try to use that approach stackoverflow.com/questions/52007482/…

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.