Python Logging, Not Writing Timestamp to Output File - python-2.7

I am creating a log using the following statement:
module = sys.modules['__main__'].__file__
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
format='%(asctime)s %(name)s (%(levelname)s): %(message)s')
log = logging.getLogger(module)
I then have an option on my script to write the output to a file using an argument -o. I think it's an issue with this statement that occurs when a person specifies that they want the log written to disk:
if arguments.o:
fh = RotatingFileHandler(arguments.o, mode='a', maxBytes=2*1024*1024,
backupCount=2, encoding=None, delay=0)
fh.setLevel(logging.DEBUG)
log.addHandler(fh)
Without adding an additional datetime statement to the text string, is there a way to do this using the format parameter?
Here is what is produced on print:
2015-11-06 07:27:13,592 C:\GIS\move_content\src\updatecontent.py (INFO): Reading Configuration file
Here is what is the configuration outputs:
Reading Configuration file
Thank you
Answer I found:
Based on my one response I was able to add this to my code, and the output now looks correct:
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)

File handler uses different format string, you should set it manually to file handler too.
Example:
fh.setFormatter(formatter)
Cookbook for Python 3 (Python 2 is the same for this question)

Related

Django: Logging to custom files every day

I'm running Django 3.1 on Docker and I want to log to different files everyday. I have a couple of crons running and also celery tasks. I don't want to log to one file because a lot of processes will be writing and debugging/reading the file will be difficult.
If I have cron tasks my_cron_1, my_cron_2,my_cron_3
I want to be able to log to a file and append the date
MyCron1_2020-12-14.log
MyCron2_2020-12-14.log
MyCron3_2020-12-14.log
MyCron1_2020-12-15.log
MyCron2_2020-12-15.log
MyCron3_2020-12-15.log
MyCron1_2020-12-16.log
MyCron2_2020-12-16.log
MyCron3_2020-12-16.log
Basically, I want to be able to pass in a name to a function that will write to a log file.
Right now I have a class MyLogger
import logging
class MyLogger:
def __init__(self,filename):
# Gets or creates a logger
self._filename = filename
def log(self,message):
message = str(message)
print(message)
logger = logging.getLogger(__name__)
# set log level
logger.setLevel(logging.DEBUG)
# define file handler and set formatter
file_handler = logging.FileHandler('logs/'+self._filename+'.log')
#formatter = logging.Formatter('%(asctime)s : %(levelname)s: %(message)s')
formatter = logging.Formatter('%(asctime)s : %(message)s')
file_handler.setFormatter(formatter)
# add file handler to logger
logger.addHandler(file_handler)
# Logs
logger.info(message)
I call the class like this
logger = MyLogger("FirstLogFile_2020-12-14")
logger.log("ONE")
logger1 = MyLogger("SecondLogFile_2020-12-14")
logger1.log("TWO")
FirstLogFile_2020-12-14 will have ONE TWO but it should only have ONE
SecondLogFile_2020-12-14 will have TWO
Why is this? Why are the logs being written to the incorrect file? What's wrong with my code?

how to get PID and export it to a log file

im trying to log the process that are running with this code.
I was able to print out but im unsure how to input this into a log file
iv looked at sample code and have gotten one some code to log but that was with some static variables.
if I remove logging.debug and put in print it will
import logging
import win32com.client
logging.basicConfig(
filename="test1.log",
level=logging.DEBUG,
format="%(asctime)s:%(levelname)s:%(message)s"
)
wmi=win32com.client.GetObject('winmgmts:')
for p in wmi.InstancesOf('win32_process'):
logging.debug ("p.Name", p.Properties_('ProcessId')), \
int(p.Properties_('UserModeTime').Value)+int(p.Properties_('KernelModeTime').Value)
children=wmi.ExecQuery('Select * from win32_process where ParentProcessId=%s' %p.Properties_('ProcessId'))
thank you in advance with any and all help
im expecting to put a timestamp with the PID
You should take a look at the official docs
If you define your logging config before executing .info, .debug etc
logging.basicConfig(filename='example.log', level=logging.DEBUG)
It will log to a file.
Logging variables should look something like this:
logging.warning('%s before you %s', 'Look', 'leap!')
In your case, use %s for Strings, %d for Integers

Create zip file of logging file using python module logging

I want to create a zip file of logging file. I have created logging file using python module logging and RotatingFileHandler.
import logging
from logging.handlers import RotatingFileHandler
# create a logging format
log_formatter = logging.Formatter('Date: %(asctime)s - %(message)s')
logFile = scheduler_name + "_"+ scheduler_id+".log"
# create a file handler
myhandler = RotatingFileHandler(logFile, mode='a', maxBytes=5*1024*1024,
backupCount=2, encoding=None, delay=0)
myhandler.setFormatter(log_formatter)
myhandler.setLevel(logging.INFO)
# add the handlers to the logger
app_log = logging.getLogger()
app_log.addHandler(myhandler)
Using that I have created a logging file and I want to create zip file using logging module inbuilt functionality
I didnt try, but possibly it should be like this. Take care of dst_file_name to generate dynamically just like your 'logFile'
import commands
myhandler.doRollover()
for i in range(self.backupCount, 0, -1):
dst_file_name='myzip.zip'
src_file_name=scheduler_name + "_"+i+".log"
cmd = "zip %s %s" %(dst_file_name, src_file_name)
commands.getoutput(cmd)
enter code here

Python logger is printing messages twice on both the PyCharm console and the log file

Similar questions were caused by the logger getting called twice. So maybe in my case it is caused by the second getLogger(). Any ideas how I can fix it?
import logging
import logging.handlers
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
"the_log.log", maxBytes=3000000, backupCount=2)
formatter = logging.Formatter(
'[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# This is required to print messages to the console as well as the log file.
logging.getLogger().addHandler(logging.StreamHandler())
Using a config file. e.g. logging.config.fileConfig('logging.ini')
[logger_root]
level=ERROR
handlers=stream_handler
[logger_webserver]
level=DEBUG
handlers=stream_handler
qualname=webserver
propagate=0
You have to set logger.propagate = 0 (Python 3 Docs) when you're configuring the root logger and using non-root-loggers at the same time.
I know this was asked a long time ago, but it's the top result on DuckDuckGo.

Python Logging -- Accessing Data within Requests Log

I'm trying to access the data within the logs that are printed. Here's how to set up logging:
import logging
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
#Initialize Logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
When I make an HTTP request using Python Requests,
r = requests.get(api+'download?name=XXXX),headers=header)
it prints out a bunch of lines of log. This is the one I'm interested in:
header: Location: http://09.bm-data-api.prod.XXXXXXX.net/download/XXXXX
How do I pass this header info back into a variable to use?
Thanks
Dave
The value you need has been logged (presumably to a file). So you'll need to read the contents of the logfile, find the line(s) you're interested in and perform the necessary work that you wish to do. You can read a file with:
with open(myfile) as f:
data = f.readlines()
and you can then iterate over data to find the lines that you're interested in.