Package joy :: Module loggit :: Class LogWriter
[hide private]
[frames] | no frames]

Class LogWriter

source code


Concrete class LogWriter provides an interface for logging data.

Logs may be written to a stream or a file. The default file format is a gzipped YAML file. If output is sent to a stream, the output is an uncompressed YAML.

Each .write() operation maps into a single YAML document, allowing the yaml module's safe_load_all method to parse them one at a time.

Typical usage: >>> L = LogWriter('mylog.yml.gz') >>> L.write( foo = 'fu', bar = 'bar' ) >>> L.close() >>> for data in iterlog('mylog.yml.gz'): >>> print data

Instance Methods [hide private]
 
__init__(self, stream=None, sync=False)
INPUTS: stream -- stream -- output stream to use, must support write, flush, close -- str -- filename to use (.gz added automatically if not present) -- None -- logger started in uninitialized state; use open() sync -- boolean -- set to force a flush after every log entry.
source code
 
open(self, stream)
Open a logging stream.
source code
 
write(self, topic, **kw)
Write a log entry.
source code
 
close(self)
Close a logger output stream
source code
 
flush(self)
Flush the logger output stream
source code
 
getterWrapperFor(self, fun, fmt=lambda x: x, attr={})
Wrap the specified callable and log results of calls.
source code
 
setterWrapperFor(self, fun, ifmt=lambda x: x, ofmt=None, attr={})
Wrap the specified callable and log parameters of calls.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, stream=None, sync=False)
(Constructor)

source code 

INPUTS:
  stream -- stream -- output stream to use, must support write, flush, close
         -- str -- filename to use (.gz added automatically if not present)
         -- None -- logger started in uninitialized state; use open()
  sync -- boolean -- set to force a flush after every log entry.

ATTRIBUTES:
  .s -- the open stream
  .sync -- auto-flush flag
  .timeUnit -- time unit for log timestamps; default is millisecond

Overrides: object.__init__

open(self, stream)

source code 

Open a logging stream. 
INPUT:
  stream -- str -- a file name for the log. A .gz will be appended if not
            present. Log will be stored as a gzip YAML file.
         -- file-like -- an object with .write() .flush() and .close() 
            methods. Log will be sent as YAML text, with a single .write()
            for each entry of the log.

write(self, topic, **kw)

source code 

Write a log entry. Each entry consists of a topic string and an automatically generated timestamp, and may include a dictionary of additional attributes supplied as keyword arguments.

The log entry will be emitted as a YAML document (entry starting with ---) that contains a mapping with keys TIME for the timestamp (in units of .timeUnit) and TOPIC for the topic.

If .sync is set, the stream will be flushed after each entry.

getterWrapperFor(self, fun, fmt=lambda x: x, attr={})

source code 

Wrap the specified callable and log results of calls.
INPUTS:
  fun -- callable -- "getter" function with no params
  fmt -- callable -- formatting function for the parameter. 
            Must return something yaml can safe_dump()
  attr -- dict -- dictionary of extra attributes for log entry 
OUTPUTS:
  callable function that calls fun() and logs the results   
  
Typical usage is to take an existing getter function and replace it in 
in place with the wrapped getter:
>>> def dummyGetter():
>>>   return int(raw_input())
>>> L = joy.loggit.LogWriter("foo")
>>> g = L.getterWrapperFor(dummyGetter
    ,fmt=lambda x : "0x%X" % x
    , attr=dict(sparrow = 'african'))
>>> g()
258
258
>>> L.close()
>>> !gunzip -c foo.gz
--- {TIME: ????, TOPIC: getter, func_name: dummyGetter, sparrow: african, value: '0x102'}

setterWrapperFor(self, fun, ifmt=lambda x: x, ofmt=None, attr={})

source code 

Wrap the specified callable and log parameters of calls.
INPUTS:
  fun -- callable function with one parameter
  name -- function name to use in 
  ifmt -- formatting function for arguments
  ofmt -- formatting function for results / None to omit result
OUTPUTS:
  callable function that logs the parameter and then
  calls fun() with it and logs the results    

Usage -- see example for getterWrapperFor