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?!?!

Deepak
  • 962
  • 4
  • 17
  • 38
  • 1
    Mabe have a look here: https://docs.python.org/3/library/logging.html – Reblochon Masque Oct 01 '19 at 06: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 – Deepak Oct 01 '19 at 07: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 – Deepak Oct 01 '19 at 16:48

1 Answers1

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.

4xel
  • 141
  • 4
  • You may also try to use that approach https://stackoverflow.com/questions/52007482/logging-to-separate-files-in-python – 4xel Oct 03 '19 at 06:31