Rafael Higa
Rafael Higa

Reputation: 725

logging two scripts in same file

Sorry, it is a simple question, but I'm not understanding well what I have to do. There are two scripts: main.py and methods.py

main.py

import methods


methods.fnc_1()


methods.fnc_2()

methods.py

import logging

logger = logging.getLogger(__name__)
f_handler = logging.FileHandler('file.log')
f_handler.setLevel(logging.DEBUG)
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.DEBUG)

logger.addHandler(c_handler)
logger.addHandler(f_handler)

def fnc_1():
    logger.warning('warning_1!')

def fnc_2():
    logger.warning('warning_2!')

How can I use the same logger object in main.py? So, I call main.py and get every log message from either main.py or methods.py in the same file, shown at the order of execution?

Upvotes: 0

Views: 165

Answers (1)

Barmar
Barmar

Reputation: 781887

Use methods.logger in main.py to get the variable from the methods module.

import methods

methods.fnc_1()
methods.logger.warning("warning from main!")
methods.fnc_2()

Upvotes: 1

Related Questions