# logging --- /var/log/syslog --- Dec 27 08:07:41 tcloudost-VirtualBox NetworkManager[738]: Activation (eth0) Stage 4 of 5 (IPv6 Configure Timeout) scheduled... Dec 27 08:07:41 tcloudost-VirtualBox NetworkManager[738]: Activation (eth0) Stage 4 of 5 (IPv6 Configure Timeout) started... Dec 27 08:07:41 tcloudost-VirtualBox NetworkManager[738]: Activation (eth0) Stage 4 of 5 (IPv6 Configure Timeout) complete. Dec 27 08:17:01 tcloudost-VirtualBox CRON[6867]: (tcloudost) CMD (/home/tcloudost/Documents/git_repositories/python-batches/batch-56/first.py) Dec 27 08:17:01 tcloudost-VirtualBox CRON[6868]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Dec 27 08:17:01 tcloudost-VirtualBox postfix/pickup[6784]: E254B165874: uid=1000 from= Dec 27 08:17:01 tcloudost-VirtualBox postfix/cleanup[6873]: E254B165874: message-id=<20161227024701.E254B165874@tcloudost-VirtualBox>

In [ ]:
# java,perl
# logging -> logj4
# https://docs.python.org/2/howto/logging.html
# https://docs.python.org/2/howto/logging-cookbook.html

In [1]:
import logging as l

In [2]:
print dir(l)


['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'FATAL', 'FileHandler', 'Filter', 'Filterer', 'Formatter', 'Handler', 'INFO', 'LogRecord', 'Logger', 'LoggerAdapter', 'Manager', 'NOTSET', 'NullHandler', 'PlaceHolder', 'RootLogger', 'StreamHandler', 'WARN', 'WARNING', '__all__', '__author__', '__builtins__', '__date__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__status__', '__version__', '_acquireLock', '_addHandlerRef', '_checkLevel', '_defaultFormatter', '_handlerList', '_handlers', '_levelNames', '_lock', '_loggerClass', '_releaseLock', '_removeHandlerRef', '_showwarning', '_srcfile', '_startTime', '_unicode', '_warnings_showwarning', 'addLevelName', 'atexit', 'basicConfig', 'cStringIO', 'captureWarnings', 'codecs', 'critical', 'currentframe', 'debug', 'disable', 'error', 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'handlers', 'info', 'log', 'logMultiprocessing', 'logProcesses', 'logThreads', 'makeLogRecord', 'os', 'raiseExceptions', 'root', 'setLoggerClass', 'shutdown', 'sys', 'thread', 'threading', 'time', 'traceback', 'warn', 'warning', 'warnings', 'weakref']

In [ ]:
# function
# debug()
# ex:
# ssh -v localhost
# ssh -vv localhost
# ssh -vvv localhost
# info()
# warning()
# error()
# critical()

# levels
# lowest to hightest
# debug,info,warning,error,critical (all in UPPER CASE)

In [4]:
l.debug("this is a debug message")
l.info("this is an info message")
l.warning("this is an warning message")
l.error("this is an error message")
l.critical("this is an critical message")


WARNING:root:this is an warning message
ERROR:root:this is an error message
CRITICAL:root:this is an critical message

In [ ]:
# The default level is WARNING, which means that only events of this 
# level and above will be tracked,
# unless the logging package is configured to do otherwise.
# to get the debug and info messages we have to configure them.

In [5]:
print help(l.basicConfig)


Help on function basicConfig in module logging:

basicConfig(**kwargs)
    Do basic configuration for the logging system.
    
    This function does nothing if the root logger already has handlers
    configured. It is a convenience method intended for use by simple scripts
    to do one-shot configuration of the logging package.
    
    The default behaviour is to create a StreamHandler which writes to
    sys.stderr, set a formatter using the BASIC_FORMAT format string, and
    add the handler to the root logger.
    
    A number of optional keyword arguments may be specified, which can alter
    the default behaviour.
    
    filename  Specifies that a FileHandler be created, using the specified
              filename, rather than a StreamHandler.
    filemode  Specifies the mode to open the file, if filename is specified
              (if filemode is unspecified, it defaults to 'a').
    format    Use the specified format string for the handler.
    datefmt   Use the specified date/time format.
    level     Set the root logger level to the specified level.
    stream    Use the specified stream to initialize the StreamHandler. Note
              that this argument is incompatible with 'filename' - if both
              are present, 'stream' is ignored.
    
    Note that you could specify a stream created using open(filename, mode)
    rather than passing the filename and mode in. However, it should be
    remembered that StreamHandler does not close its stream (since it may be
    using sys.stdout or sys.stderr), whereas FileHandler closes its stream
    when the handler is closed.

None

In [6]:
print help(l.Formatter)


Help on class Formatter in module logging:

class Formatter(__builtin__.object)
 |  Formatter instances are used to convert a LogRecord to text.
 |  
 |  Formatters need to know how a LogRecord is constructed. They are
 |  responsible for converting a LogRecord to (usually) a string which can
 |  be interpreted by either a human or an external system. The base Formatter
 |  allows a formatting string to be specified. If none is supplied, the
 |  default value of "%s(message)\n" is used.
 |  
 |  The Formatter can be initialized with a format string which makes use of
 |  knowledge of the LogRecord attributes - e.g. the default value mentioned
 |  above makes use of the fact that the user's message and arguments are pre-
 |  formatted into a LogRecord's message attribute. Currently, the useful
 |  attributes in a LogRecord are described by:
 |  
 |  %(name)s            Name of the logger (logging channel)
 |  %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
 |                      WARNING, ERROR, CRITICAL)
 |  %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
 |                      "WARNING", "ERROR", "CRITICAL")
 |  %(pathname)s        Full pathname of the source file where the logging
 |                      call was issued (if available)
 |  %(filename)s        Filename portion of pathname
 |  %(module)s          Module (name portion of filename)
 |  %(lineno)d          Source line number where the logging call was issued
 |                      (if available)
 |  %(funcName)s        Function name
 |  %(created)f         Time when the LogRecord was created (time.time()
 |                      return value)
 |  %(asctime)s         Textual time when the LogRecord was created
 |  %(msecs)d           Millisecond portion of the creation time
 |  %(relativeCreated)d Time in milliseconds when the LogRecord was created,
 |                      relative to the time the logging module was loaded
 |                      (typically at application startup time)
 |  %(thread)d          Thread ID (if available)
 |  %(threadName)s      Thread name (if available)
 |  %(process)d         Process ID (if available)
 |  %(message)s         The result of record.getMessage(), computed just as
 |                      the record is emitted
 |  
 |  Methods defined here:
 |  
 |  __init__(self, fmt=None, datefmt=None)
 |      Initialize the formatter with specified format strings.
 |      
 |      Initialize the formatter either with the specified format string, or a
 |      default as described above. Allow for specialized date formatting with
 |      the optional datefmt argument (if omitted, you get the ISO8601 format).
 |  
 |  format(self, record)
 |      Format the specified record as text.
 |      
 |      The record's attribute dictionary is used as the operand to a
 |      string formatting operation which yields the returned string.
 |      Before formatting the dictionary, a couple of preparatory steps
 |      are carried out. The message attribute of the record is computed
 |      using LogRecord.getMessage(). If the formatting string uses the
 |      time (as determined by a call to usesTime(), formatTime() is
 |      called to format the event time. If there is exception information,
 |      it is formatted using formatException() and appended to the message.
 |  
 |  formatException(self, ei)
 |      Format and return the specified exception information as a string.
 |      
 |      This default implementation just uses
 |      traceback.print_exception()
 |  
 |  formatTime(self, record, datefmt=None)
 |      Return the creation time of the specified LogRecord as formatted text.
 |      
 |      This method should be called from format() by a formatter which
 |      wants to make use of a formatted time. This method can be overridden
 |      in formatters to provide for any specific requirement, but the
 |      basic behaviour is as follows: if datefmt (a string) is specified,
 |      it is used with time.strftime() to format the creation time of the
 |      record. Otherwise, the ISO8601 format is used. The resulting
 |      string is returned. This function uses a user-configurable function
 |      to convert the creation time to a tuple. By default, time.localtime()
 |      is used; to change this for a particular formatter instance, set the
 |      'converter' attribute to a function with the same signature as
 |      time.localtime() or time.gmtime(). To change it for all formatters,
 |      for example if you want all logging times to be shown in GMT,
 |      set the 'converter' attribute in the Formatter class.
 |  
 |  usesTime(self)
 |      Check if the format uses the creation time of the record.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  converter = <built-in function localtime>
 |      localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
 |                                tm_sec,tm_wday,tm_yday,tm_isdst)
 |      
 |      Convert seconds since the Epoch to a time tuple expressing local time.
 |      When 'seconds' is not passed in, convert the current time instead.

None

In [ ]:
# man date