commotion_client.utils.logger.LogHandler Class Reference
Inheritance diagram for commotion_client.utils.logger.LogHandler:

Public Member Functions

def __init__
 
def set_logfile
 
def set_verbosity
 
def get_logger
 

Public Attributes

 logger
 
 levels
 
 formatter
 
 stream
 
 file_handler
 
 logfile
 

Detailed Description

Main logging controls for Commotion-Client. 

This application is ONLY to be called by the main application. This logger sets up the main namespace for all other logging to take place within. All other loggers should be the core string "commotion_client" and the packages __name__ to use inheretance from the main commotion package. This way the code in an indivdual extension will be small and will inheret the logging settings that were defined in the main application.

Example Use for ALL other modules and packages:

    from commotion-client.utils import logger
    log = logger.getLogger("commotion_client"+__name__)


NOTE: The exceptions in this function do not have translation implemented. This is that they are called before the QT application and, as such, are not pushed through QT's translation tools. This could be a mistake on the developers side, as he is a bit foggy on the specifics of QT translation. You can access the feature request at https://github.com/opentechinstitute/commotion-client/issues/24

Member Function Documentation

def commotion_client.utils.logger.LogHandler.set_logfile (   self,
  logfile = None 
)
Set the file to log to.

Args:
logfile (string): The absolute path to the file to log to.
  optional: defaults to the default system logfile path.

References commotion_client.utils.logger.LogHandler.logfile.

60 
61  def set_logfile(self, logfile=None):
62  """Set the file to log to.
63 
64  Args:
65  logfile (string): The absolute path to the file to log to.
66  optional: defaults to the default system logfile path.
67  """
68  if logfile:
69  log_dir = QtCore.QDir(os.path.dirname(logfile))
70  if not log_dir.exists():
71  if log_dir.mkpath(log_dir.absolutePath()):
72  self.logfile = logfile
73  platform = sys.platform
74  if platform == 'darwin':
75  #Try <user>/Library/Logs first
76  log_dir = QtCore.QDir(os.path.join(QtCore.QDir.homePath(), "Library", "Logs"))
77  #if it does not exist try and create it
78  if not log_dir.exists():
79  if not log_dir.mkpath(log_dir.absolutePath()):
80  raise NotADirectoryError("Attempted to set logging to the user's Commotion directory. The directory '<home>/.Commotion' does not exist and could not be created.")
81  self.logfile = log_dir.filePath("commotion.log")
82  elif platform in ['win32', 'cygwin']:
83  #Try ../AppData/Local/Commotion first
84  log_dir = QtCore.QDir(os.path.join(os.getenv('APPDATA'), "Local", "Commotion"))
85  #if it does not exist try and create it
86  if not log_dir.exists():
87  if not log_dir.mkpath(log_dir.absolutePath()):
88  raise NotADirectoryError("Attempted to set logging to the user's Commotion directory. The directory '<home>/.Commotion' does not exist and could not be created.")
89  self.logfile = log_dir.filePath("commotion.log")
90  elif platform == 'linux':
91  #Try /var/logs/
92  log_dir = QtCore.QDir("/var/logs/")
93  if not log_dir.exists(): #Seriously! What kind of twisted linux system is this?
94  if log_dir.mkpath(log_dir.absolutePath()):
95  self.logfile = log_dir.filePath("commotion.log")
96  else:
97  #If fail then just write logs in home directory
98  #TODO check if this is appropriate... its not.
99  home = QtCore.QDir.home()
100  if not home.exists(".Commotion") and not home.mkdir(".Commotion"):
101  raise NotADirectoryError("Attempted to set logging to the user's Commotion directory. The directory '{0}/.Commotion' does not exist and could not be created.".format(home.absolutePath()))
102  else:
103  home.cd(".Commotion")
104  self.logfile = home.filePath("commotion.log")
105  else:
106  self.logfile = log_dir.filePath("commotion.log")
107  else:
108  #I'm out!
109  raise OSError("Could not create a logfile.")
def set_logfile
Definition: logger.py:60
def commotion_client.utils.logger.LogHandler.set_verbosity (   self,
  verbosity = None,
  log_type = None 
)
Set's the verbosity of the logging for the application.

Args:
  verbosity (string|int): The verbosity level for logging to take place.
    optional: Defaults to "Error" level
  log_type (string): The type of logging whose verbosity is to be changed.
    optional: If not specified ALL logging types will be changed.

Returns:
  bool True if successful, False if failed

Raises:
exception: Description.

References commotion_client.utils.logger.LogHandler.file_handler, commotion_client.utils.logger.LogHandler.formatter, commotion_client.utils.logger.LogHandler.levels, commotion_client.utils.logger.LogHandler.logfile, commotion_client.utils.logger.LogHandler.logger, commotion_client.commotion_client.CommotionClientApplication.logger, and commotion_client.utils.logger.LogHandler.stream.

111  def set_verbosity(self, verbosity=None, log_type=None):
112  """Set's the verbosity of the logging for the application.
113 
114  Args:
115  verbosity (string|int): The verbosity level for logging to take place.
116  optional: Defaults to "Error" level
117  log_type (string): The type of logging whose verbosity is to be changed.
118  optional: If not specified ALL logging types will be changed.
119 
120  Returns:
121  bool True if successful, False if failed
122 
123  Raises:
124  exception: Description.
125 
126  """
127  try:
128  int_level = int(verbosity)
129  except ValueError:
130  if str(verbosity).upper() in self.levels.keys():
131  level = self.levels[str(verbosity).upper()]
132  else:
133  return False
134  else:
135  if 1 <= int_level <= 5:
136  _levels = [ 'CRITICAL', 'ERROR', 'WARN', 'INFO', 'DEBUG']
137  level = self.levels[_levels[int_level-1]]
138  else:
139  return False
140 
141  if log_type == "stream":
142  set_stream = True
143  elif log_type == "logfile":
144  set_logfile = True
145  else:
146  set_logfile = True
147  set_stream = True
148 
149  if set_stream == True:
150  self.logger.removeHandler(self.stream)
151  self.stream = None
152  self.stream = logging.StreamHandler()
153  self.stream.setFormatter(self.formatter)
154  self.stream.setLevel(level)
155  self.logger.addHandler(self.stream)
156  if set_logfile == True:
157  self.logger.removeHandler(self.file_handler)
158  self.file_handler = None
159  self.file_handler = handlers.RotatingFileHandler(self.logfile,
160  maxBytes=5000000,
161  backupCount=5)
162  self.file_handler.setFormatter(self.formatter)
163  self.file_handler.setLevel(level)
164  self.logger.addHandler(self.file_handler)
165  return True
file_handler
Definition: logger.py:54
def set_verbosity
Definition: logger.py:110

The documentation for this class was generated from the following file:
  • commotion_client/utils/logger.py