Ask Your Question
1

How can I make two separate run files log information to a single sweep?

asked 2022-12-28 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-11-27 12:00:00 +0000

lakamha gravatar image

One way to achieve this is to use a single logging library to log information from both run files. This can be done by creating a logger instance in each run file using the same configuration, such as the same log file or logging level. Then, use the same name for both logger instances, which will cause them to refer to the same logger object.

For example, in both run files:

import logging

# create logger
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)

# create file handler
fh = logging.FileHandler('my_log_file.log')
fh.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)

# add handler to logger
logger.addHandler(fh)

# log information
logger.debug('Hello, world!')

By using the same logger name ('mylogger') in both run files, the logger objects will refer to the same logger, and all log messages from both run files will be stored in the same file ('mylog_file.log').

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-28 11:00:00 +0000

Seen: 7 times

Last updated: Nov 27 '22