[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2

Introduction

This is the story when using logrotate with logging in python.

In python2 (it can't be helped), I specified hands in logging.basicConfig and used TimedRotatingFileHandler, but for some reason it wasn't even written to the specified log file, and of course log rotation wasn't done either. But when I run it in python3 it works.

I thought I wasn't sure, so I solved it in a different way, so I'll make a note of it.

Someone said that it doesn't work with some versions of python2, Do you specify this in the first place? I would appreciate it if you could give me some information.

** I wrote it in the postscript, but I got information from the inside of the company and it became clear why it did not work! Thank you. .. ** **

Contents

Conclusion

In conclusion, biting TimedRotatingFileHandler into addHandler () worked fine.

import logging
from logging.handlers import TimedRotatingFileHandler 


logger = logging.getLogger(__name__)
formatter = "%(asctime)s:%(name)s:%(levelname)s:%(message)s"
handler = TimedRotatingFileHandler(
	"/hogehoge/logger.log",
	when="M",
	backupCount=1
)

logger.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter(formatter))
logger.addHandler(handler)

logger.info("===== START HOGEHOGE =====")

Details

The sources that didn't work are:

import logging
import logging.handlers

logger = logging.getLogger(__name__)
formatter = '%(asctime)s:%(name)s:%(levelname)s:%(message)s'
logging.basicConfig(
    level=logging.DEBUG,
    format=formatter,
    handlers=[logging.handlers.TimedRotatingFileHandler(
        filename ='/hogehoge/logger.log',
        when="M",
        backupCount=1
    )]
)

logger.setLevel(logging.INFO)
logger.info("=== START HOGEHOGE ===")

The cause was unknown, but if the above method did not work for the time being, I'm thinking of feeding addHandler.

Postscript

Depending on the environment, I thought about doing TimedRotatingFileHandler with python2, I set handlers in basicConfig and it didn't work, The reason was apparently not simply supported. .. ..

Certainly, you can see that there are no handlers in the following contents and it cannot be used with basicConfig. It took me a while to read more documents. I learned a lot.

##Contents
The following keyword arguments are supported.

・ Filename
A FileHandler is created with the specified name instead of StreamHandler.

・ Filemode
If filename is specified, open the file in this mode. Defaults to 'a'.

・ Format
Uses the specified formatted string in the handler.

・ Datefmt
Time in the specified date and time format.strftime()Use what is accepted by.

・ Level
Sets the level of the root logger to the specified level.

・ Stream
Use the specified stream to initialize the StreamHandler. Note that this argument is incompatible with filename - if both are present, stream is ignored.

##reference
https://docs.python.org/ja/2.7/library/logging.html

Recommended Posts

[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2
Patch when full text search does not work in GAE / Python local environment
Command when ACPI shutdown does not work in VirtualBox
Precautions when using pit in Python
Delete a particular character in Python if it is the last
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
Check points when MIDI does not work in a program using SDL_mixer
python> does not include the letters mm> if "mm" not in text: / print "not including mm"
Check items when the imported python module does not work as expected
python note: when easy_install is not available
Precautions when pickling a function in python
[VScode] autopep8 format does not work [Python]
Virtualenv does not work on Python3.5 (Windows)
Python / dictionary> setdefault ()> Add if not in dictionary
Even if the development language is changed to python3 in Cloud9, version 2 is displayed in python --version
[Python] Precautions when retrieving data by scraping and putting it in the list
A story that sometimes does not work if pip is up to date
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
When I try matplotlib in Python, it says'cairo.Context'
When searching is not working in GAE's Datastore
Precautions when dealing with control structures in Python 2.6
Jinja2 2.9.6 does not work on Lambda Python 3 series
When wildcard specification does not work with pylint
It is said that libmysqlclient.so.18 does not exist
What to do when python3 type venv does not work well on Raspberry Pi
What to do if the progress bar is not displayed in tqdm of python
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do if Python does not switch from the System version in pyenv
Check if the string is a number in python
When "Message: session not created" occurs in Python + Selenium
When the selected object in bpy.context.selected_objects is not returned
Key input that does not wait for key input in Python
Precautions when dealing with ROS MultiArray types in Python
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
When "ERROR: HTTP is not supported." Is displayed in mpsyt
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
About the matter that nosetests does not pass when __init__.py is created in the project directory
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
[Python beginner] Variables and scope inside the function (when the processing inside the function is reflected outside the function and when it is not reflected)