spyder: logging library doesn't seem to work with spyder

Hello,

I’m trying to run a script using python standard library ‘logging’. This script works fine from the command line but when run from Spyder, no log is printed.

My configuration is: Windows 7 Python 2.7.10 :: Anaconda 2.3.0 (64-bit) spyder-app 2.3.5.2

The script is:

# -*- coding: utf-8 -*-
"""
Simple test using logging
"""
import logging

# Logger configuration:
log_level = logging.DEBUG 
log_format = '%(levelname)s:%(message)s'

# Testing logger
logger = logging.root
logger.basicConfig = logging.basicConfig(format=log_format, filename='test.log', level=log_level)
logger.info('Testing logger (info)')
logger.debug('Testing logger (debug)')
logger.warning('Testing logger (warn)')
logger.info("")

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 34 (13 by maintainers)

Most upvoted comments

Thanks! force=True worked on my system Mac, Python 3.8.1, Spyder 4.1.3)

@gokhalen, your use case also seems fixed in Python 3.8, by passing a new parameter called force=True to basicConfig so it can be reset each time you run new code.

A bug that I encounterd with spyder and logging is that if you run a script that logs multiple times, it will log as many times as you have run the script.

So I ran a script 4 times, and it printed:

[2019-01-13 13:47:10] INFO --- Started (trades.py:26) [2019-01-13 13:47:10] INFO --- Started (trades.py:26) [2019-01-13 13:47:10] INFO --- Started (trades.py:26) [2019-01-13 13:47:10] INFO --- Started (trades.py:26)

Any ideas on how to remove this?

Also, when run a script with logging that outputs to a file, you need to close spyder to delete that file.

The code snippet above (https://github.com/spyder-ide/spyder/issues/2572#issue-98023025) still won’t work as of today (2017/10/5) with the latest Anaconda 64 bit Python 3.6 under Windows 10 64 bit.

Python 3.6.2 IPython 6.1.0 Spyder 3.2.3 ipykernel.__version__ == '4.6.1'