System i Blogger

Jan 9 2009   4:58PM GMT

Fooling arond with WORDPRESS



Posted by: David Vasta
Tags:
#!/usr/bin/env python
"""
Module simplelog :

provides immediate logging to a text file (using module logging)

Usage:

from simplelog import *
log = getSimpleLogger("myPath/myFile.txt")
log.info("an example message")

Remark:

getSimpleLogger can be called multiple times with the same
filename. It identifies the logger with the file's basename,
so you can't have 2 different loggers named myFile.txt (even
in different folders.)
"""

import logging 
import os .path 
__all__ =["getSimpleLogger"]
_loggers ={}

def getSimpleLogger (pathToFile ):
  "returns a logger to the given file"
  name =os .path .basename (pathToFile )
  theLogger =logging .getLogger (name )
  if name in _loggers :
    return theLogger 
  _loggers [name ]=True 
  theLogger .setLevel (logging .DEBUG )
  theHandler =logging .FileHandler (pathToFile ,'w')
  theFormatter =logging .Formatter (
  "%(asctime)s %(levelname)-8s %(message)s",
  "%H:%M:%S",
#"%a, %d %b %Y %H:%M:%S",
  )
  theHandler .setFormatter (theFormatter )
  theLogger .addHandler (theHandler )
  return theLogger 

if __name__ =="__main__":
  logger =getSimpleLogger ("foo.txt")
  for i in range (20 ):
    logger .info ("current index is %d"%i )
patterns = {
'hello' => /hello/ ,
'hi' => /hi/,
'time' => /\d\d:\d\d:\d\d/,
'date' => /\d\d\/\d\d\/\d\d\d\d/,
'email' => /^[0-9a-zA-Z.!#\$%^]+@[a-zA-Z]+\.[a-z]+$/
}

line = ''
while line !~ /(^quit$|^Quit$|^q$|^Q$)/
  print "\n (q to exit) >"
  line = gets
  patterns.each do |key, value|
    puts "#{key} line= #{line}" if line =~ patterns[key]
  end
end
puts 'Bye'

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: