log.py :  » Game-2D-3D » PsychoPy » PsychoPy-0.96.02 » psychopy » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PsychoPy 
PsychoPy » PsychoPy 0.96.02 » psychopy » log.py
"""Provides functions for logging error and other messages to one or more
files and/or the console, using python's own logging module. Some warning
messages and error messages are generated by PsychoPy itself. The user can
generate more using the functions in this module.

There are various levels for logged messages; ranging from ERROR, through
WARNING, to INFO and DEBUG. When setting the level for a particular log
(file or console) the  user can set the minimum level that is required 
for messages to enter the log. For example, setting a level of INFO will
result in INFO, WARNING and ERROR messages to be recorded but not DEBUG 
nessages.

By default, PsychoPy will record messages of WARNING level and above to
the console and INFO upwards to a psychopy.log in the current directory. The user
can silence both by setting them to receive only CRITICAL messages, which 
(PsychoPy doesn't use) using the commands; 
::

    from psychopy import log
    log.psychopyLog.setLevel(log.CRITICAL)
    log.console.setLevel(log.CRITICAL)

"""
from os import path
import logging
_packagePath = path.split(__file__)[0]

CRITICAL = logging.CRITICAL
ERROR = logging.ERROR#40
WARNING = logging.WARNING#30
INFO = logging.INFO#20
DEBUG = logging.DEBUG#10

error = logging.error
warning = logging.warning
info = logging.info
debug = logging.debug

console = logging.StreamHandler() #create a handler for the console
console.setFormatter(logging.Formatter('%(asctime)-s %(levelname)-8s %(message)s', '%y-%m-%d %H:%M'))
console.setLevel(ERROR)
#the default 'origin' of the log messages
_rootLogger = logging.getLogger('')
_rootLogger.addHandler(console)# add the console logger to receive all root logs
_rootLogger.setLevel(logging.DEBUG) #the minimum to be sent

class LogFile:
    """Creates an object for logging events to a file (of which is 
    psychopyLog."""
    def __init__(self, filename, 
                       filemode = 'a', level=INFO,
                       format = '%(asctime)-s %(levelname)-8s %(message)s', 
                       dateFormat='%y-%m-%d %H:%M'):
        """**Arguments:**
            - filename
            - filemode = 'a'(ppend) or 'w'(rite). The latter will remove the previous file
            - level = the minimum level of the messages to be entered into the log
            - format = a string defining the format of messages
            - datefmt = a string specifying just the date part of the message
        """
        self._log = logging.FileHandler(filename, filemode)
        #~ self._log = logging.basicConfig(level=level,
                    #~ format=format, datefmt=datefmt,
                    #~ filename=filename, filemode=filemode)
        self._log.setLevel(level)
        formatter = logging.Formatter(format, dateFormat)
        self._log.setFormatter(formatter)
        _rootLogger.addHandler(self._log)
    def setLevel(self, level):
        """Sets the current level for this log (numerically)
        The values correspond to:
        
            - 40:Error
            - 30:Warning
            - 20:Info
            - 10:Debug"""
        self._log.setLevel(level)
    def getLevel(self):
        """Returns the current level for this log (numerically)
        The values correspond to:
        
            - 40:Error
            - 30:Warning
            - 20:Info
            - 10:Debug
            """
        return self._log.level        
        
#psychopyLog = LogFile('psychopy.log', 'a', level=ERROR)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.