logging module versus writing your own

This is the place for queries that don't fit in any of the other categories.

logging module versus writing your own

Postby metulburr » Thu May 09, 2013 7:14 pm

What is the purpose of the logging module? Unless it has other features, to me it just seems easier to write my own calss, for example:
Code: Select all
import datetime

class Logger:
   def __init__(self, filename):
      self.filename = filename
      self.timestamp = datetime.datetime.now().strftime("%m/%d/%Y %r")
      
   def msg(self, stringer):
      '''write message in logging file'''
      f = open(self.filename, 'a')
      code = 'INFO'
      s = '{}; {}; {}\n'.format(self.timestamp, code, stringer)
      f.write(s)
      f.close()
      
log = Logger('logger.log')
log.msg('test')
log.msg('test 2')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: logging module versus writing your own

Postby setrofim » Thu May 09, 2013 7:36 pm

How is that easier than
Code: Select all
import logging
logging.basicConfig(format="%(asctime)s %(message)s", filename="myfile.log", level=logging.INFO)

?

You're just re-inventing the wheel. Not to mention the fact that your logger is horribly inefficient as it re-opens the file for each message (and also your timestamp is the the time of the creation of the logger and not of the message).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: logging module versus writing your own

Postby metulburr » Thu May 09, 2013 7:56 pm

oh yeah, i guess so

Is it just to minimize code of logging and errors like such, or does it have anything *extra* or special.

I dont know, it just seems weird to me that there is a specific logging module that formats the string sent to the file, as opposed to writing your own.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: logging module versus writing your own

Postby setrofim » Thu May 09, 2013 8:17 pm

logging module has a ton of features. For example, you can create a logger that automatically rotates files once they grow to a certain size. Or you can have logger with multiple handlers for different levels, e.g. so that you only have errors and warnings going to the screen but full output going to a file. You can use filters to pre-process the messages before they are output. You can even log across a network. logging is also thread-safe so you can use it from multiple threads without worrying about messages interleaving on one line. Check out the cookbook in the docs for an overview of some the commonly used features.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests

cron