logging question(s)

Posted on
Wed Jan 02, 2019 6:24 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

logging question(s)

I am trying to convert to the "standard" logging.

I have currently several formats depending on the "level" (info, error,..)
Trying to do this within self.plugin_file_handler.setFormatter(logformat), it seems that there is only one format for file and std indigo log at a time
A switch between formats for eg error and info would require

Code: Select all
logformatERROR = logging.Formatter('%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s', datefmt='%Y-%m-%d %H:%M:%S')
logformatINFO = logging.Formatter('%(asctime)s    %(msg)s', datefmt='%H:%M:%S')

### then in the code
self.plugin_file_handler.setFormatter(logformatERROR)
self.mylogger.ERROR("this is an error")
....

....
self.plugin_file_handler.setFormatter(logformatINFO)
self.mylogger.INFO("this is an info printout")

is there anyway to have several format definitions action for ONE log file, without always having to activate the ERROR/ INFO format when needed?


right now I am doing this all in one line:
if self.myLog.decide( "socket"): self.myLog.Print(text=" this is the text", mType="this goes in front of the line",errorType="small/big"), showDate=True,... )
and myLog does all the "work"
thanks

Karl

Posted on
Fri Jan 04, 2019 1:57 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: logging question(s)

If I understand your question, maybe this is the answer?

[MODERATOR NOTE] moved to the correct forum.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jan 04, 2019 5:31 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: logging question(s)

Code: Select all
   def startup(self):
      formats=   {logging.THREADDEBUG: "%(asctime)s %(msg)s",
                  logging.DEBUG:       "%(asctime)s %(msg)s",
                  logging.INFO:        "%(asctime)s %(msg)s",
                  logging.WARNING:     "%(asctime)s %(msg)s",
                  logging.ERROR:       "%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s",
                  logging.CRITICAL:    "%(asctime)s.%(msecs)03d\t%(levelname)-12s\t%(name)s.%(funcName)-25s %(msg)s" }

      date_Format = {logging.THREADDEBUG: "%d %H:%M:%S",
                  logging.DEBUG:       "%d %H:%M:%S",
                  logging.INFO:        "%H:%M:%S",
                  logging.WARNING:     "%H:%M:%S",
                  logging.ERROR:       "%Y-%m-%d %H:%M:%S",
                  logging.CRITICAL:    "%Y-%m-%d %H:%M:%S" }
      formatter = LevelFormatter(fmt="%(msg)s", datefmt="%Y-%m-%d %H:%M:%S", level_fmts=formats, level_date=date_Format)

      self.plugin_file_handler.setFormatter(formatter)
      self.myLOG = logging.getLogger("Plugin") 
      self.myLOG.setLevel(logging.THREADDEBUG)
      self.myLOG.log(5, u"-- text  5 --")
      self.myLOG.log(10,u"-- text 10 --")
      self.myLOG.info(u"-- text 20 --")
      self.myLOG.log(30,u"-- text 30 --")
      self.myLOG.log(40,u"-- text 40 --")
      self.myLOG.log(50,u"-- text 50 --")


class LevelFormatter(logging.Formatter):
   def __init__(self, fmt=None, datefmt=None, level_fmts={}, level_date={}):
      self._level_formatters = {}
      self._level_date_format = {}
      for level, format in level_fmts.items():
         # Could optionally support level names too
         self._level_formatters[level] = logging.Formatter(fmt=format, datefmt=level_date[level])  ## this is wrong in the example on the web page logging. is missing
      # self._fmt will be the default format
      super(LevelFormatter, self).__init__(fmt=fmt, datefmt=datefmt)

   def format(self, record):
      if record.levelno in self._level_formatters:
         return self._level_formatters[record.levelno].format(record)

      return super(LevelFormatter, self).format(record)
produces this:
Code: Select all
      04 17:12:52 -- text  5 --
      04 17:12:52 -- text 10 --
      17:12:52 -- text 20 --
      17:12:52 -- text 30 --
      2019-01-04 17:12:52.029   ERROR          Plugin.startup                   -- text 40 --
      2019-01-04 17:12:52.030   CRITICAL       Plugin.startup                   -- text 50 --


that is it for me = happy:
1. can set debug level (severity) and different formats for each level
2. and with
Code: Select all
 if self.decideLog("<area>"): self.methodTracer.log(5, u"-- text  5 --")
I can switch on/off ares of logging
Where
Code: Select all
def decideLog(self, area):
   if area in self.debugAreas or area == "all": return True
   return False


self.debugAreas = ["startup","comm",...] # set with config prefs


Thanks for the pointers and help

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests