question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logger printing the logs twice to console

See original GitHub issue
import logging
from sacred import Experiment
from sacred.settings import SETTINGS
SETTINGS.CAPTURE_MODE = 'no'
ex = Experiment('test')

logger = logging.getLogger('test')
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

@ex.automain
def main():
	logger.info("hahah")

output:

2018-04-12 04:32:24,363 - test - WARNING - No observers have been added to this run
WARNING - test - No observers have been added to this run
2018-04-12 04:32:24,363 - test - INFO - Running command 'main'
INFO - test - Running command 'main'
2018-04-12 04:32:24,364 - test - INFO - Started
INFO - test - Started
2018-04-12 04:32:24,364 - test - INFO - hahah
INFO - test - hahah
2018-04-12 04:32:24,364 - test - INFO - Completed after 0:00:00
INFO - test - Completed after 0:00:00

Why is everything getting printed twice? One with date and one without? Since my log can be huge, I don’t want to send them to observers.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
eastskykangcommented, Jul 20, 2018

I found the solution for my case, (tensorflow 1.8.0)

from tensorflow.python.platform import tf_logging as logging
logging._get_logger().propagate = False

simply works.

1reaction
bzamecnikcommented, Feb 1, 2019

It seems that the only problem is that sacred.utils.create_basic_stream_logger() has a hard coded format. The proper solution would be to make the format parametrizable (and provide this as default). In my case copying the function and providing different format worked well (even with other modules and tensorflow. Custom format, no log duplication.

def create_basic_stream_logger(format):
    logger = logging.getLogger('')
    logger.setLevel(logging.INFO)
    logger.handlers = []
    ch = logging.StreamHandler()
    formatter = logging.Formatter(format)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger

ex = Experiment('foo')
# custom format with date
ex.logger = create_basic_stream_logger('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Read more comments on GitHub >

github_iconTop Results From Across the Web

log messages appearing twice with Python Logging
You are calling configure_logging twice (maybe in the __init__ ... But if we had a parent logging object, then it will be printed...
Read more >
Duplicate Logging Messages in Python - jdhao's digital space
Option 2: Simply do not use root logger in main.py . For example, we can get another logger by using another_logger = logging.getLogger('main') ......
Read more >
Why Log statement is printing twice in console log file.
In some cases, logging output is executed twice to a log destination. This is a side effect of the Log4j additivity which is...
Read more >
4.2 - Console.log outputting everything twice.
The reason everything prints twice is because you're looping over the cards' properties (which there are two of) and then printing the suit...
Read more >
This Is Why React Prints All Console Logs Twice - YouTube
Full Video: https://youtu.be/XUwzASyHr4QSorry about the bad audio quality. My computer changed my mic to use my webcam mic instead of my ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found