screenlets.logger

Package screenlets :: Module logger |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.logger

     1  # This program is free software: you can redistribute it and/or modify
     2  # it under the terms of the GNU General Public License as published by
     3  # the Free Software Foundation, either version 3 of the License, or
     4  # (at your option) any later version.
     5  #
     6  # This program is distributed in the hope that it will be useful,
     7  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     9  # GNU General Public License for more details.
    10  #
    11  # You should have received a copy of the GNU General Public License
    12  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    13
    14  # Logger and command line parser for universal-applets.
    15  # (c) 2008 Przemyslaw Firszt (pefi) <pefi@epf.pl>
    16
    17  import os
    18  import sys
    19  import logging
    20
    21  import screenlets
    22
    23 -def logger_init():
    24          """Initialize logger"""
    25          options = screenlets.COMMAND_LINE_OPTIONS
    26          #create main logger
    27          log = logging.getLogger(screenlets.LOG_NAME)
    28          if not options.LOG_DISABLED:
    29                  #set log output
    30                  if options.LOG_OUTPUT == "STDOUT":
    31                          log_file = logging.StreamHandler(sys.stdout)
    32                  elif options.LOG_OUTPUT == "STDERR":
    33                          log_file = logging.StreamHandler(sys.stderr)
    34                  elif options.LOG_OUTPUT == "FILE":
    35                          try:
    36                                  log_file = logging.FileHandler(screenlets.LOG_FILE, "w")
    37                          except IOError:
    38                                  print("Cannot create %s logfile. Using STDERR instead.") %(screenlets.LOG_FILE)
    39                                  log_file = logging.StreamHandler(sys.stderr)
    40                  else:
    41                          print("Unknown output type: %s, using STDERR instead.") %(screenlets.LOG_FILE)
    42                          log_file = logging.StreamHandler(sys.stderr)
    43                  #check if LOG_LEVEL is valid and set
    44                  try:
    45                          level = 51 - (10 * int(options.LOG_LEVEL))  #multiply by 10 and substract from 51 to fit into allowed range
    46                          log.setLevel(level)
    47                          log_file.setLevel(level)
    48                  except ValueError:
    49                          print ("LOG_LEVEL %s is not allowed. Use -h to get more information. Using LOG_LEVEL=5") %(screenlets.LOG_LEVEL)
    50                          log.setLevel(1) #command  line paramete was wrong, but user wanted logging ..."1" means log everything
    51                          log_file.setLevel(1)
    52          else:
    53                  #do not log anything but we still have to provide logger to avoid errors
    54                  log_file = logging.StreamHandler(sys.stderr)
    55                  #screenlets.LOG_LEVEL = 51
    56                  log.setLevel(51)
    57                  log_file.setLevel(51)
    58
    59          #set the format of log message
    60          log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
    61          log_file.setFormatter(log_formatter)
    62
    63          #add file to logger
    64          log.addHandler(log_file)
    65          return log
    66
    67  log = logger_init()
    68
    69 -def get_default_logger():
    70          """This function returns default logger"""
    71          global log
    72          # Try to return the logger (an exception mean that the logger has not yet been initialized)
    73          try:
    74                  return log
    75          except NameError:
    76                  log = logger_init()
    77                  if log:
    78                          log.debug("%s: Logger initialized" %__name__)
    79                          return log
    80                  else:
    81                          print("%s: Cannot initialize logger!" %__name__) #TODO to quit or not to quit? Lack of logger may cause random errors
    82                          return None #TODO Raise Exception?
    83