@turboc @ReneTode
Playing off of this … I believe what you’re looking for is to have the line number and module all in the log entry, correct? This is how I would do it which I believe solves your problem. Granted, @yawor seems to have heard yours and others issues and has already implemented a PR to solve this.
Regardless, hereyougo~
###appdaemon.cfg
[custom_logger]
module = custom_logger
class = CustomLogger
[utils]
module = utils
class = utils
dependencies = custom_logger
###custom_logger.py
import appdaemon.appapi as appapi
import appdaemon.homeassistant as ha
import datetime
import inspect
import re
class CustomLogger(appapi.AppDaemon):
def initialize(self):
pass
def log(self, msg, level="INFO"):
self._log(self._logger, level, msg, self.name)
def error(self, msg, level="WARNING"):
self._log(self._error, level, msg, self.name)
def _log(self, logger, level, msg, name=""):
levels = {
"CRITICAL": 50,
"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10,
"NOTSET": 0
}
# stack returns list of FrameInfo(frame, filename, lineno, function, code_context, index)
# app is 2 frames up == stack[2]
stack = inspect.stack()
# RegEx to extract name of app from filename
re_name = re.compile('.*\/(.*).py')
if ha.conf.realtime:
timestamp = datetime.datetime.now()
else:
timestamp = ha.get_now()
message_format = ('[{t}] {lvl} {name}.{mod} line {ln} {msg}'
.format(t=timestamp,
lvl=level,
name=re_name.match(stack[2][1]).group(1),
mod=stack[2][3],
ln=stack[2][2],
msg=msg))
logger.log(levels[level], message_format)
utils.py (for example of usage)
import appdaemon.appapi as appapi
import json
class utils(appapi.AppDaemon):
def initialize(self):
self.custom_logger = self.get_app('custom_logger')
self.custom_logger.log('how bow dah')
###output logfile.log
2017-02-26 13:36:36.036629 INFO Reloading Module: /home/homeassistant/.homeassistant/appdaemon/conf/apps/custom_logger.py
2017-02-26 13:36:36.045918 INFO Loading Object custom_logger using class CustomLogger from module custom_logger
2017-02-26 13:36:36.049179 INFO Reloading Module: /home/homeassistant/.homeassistant/appdaemon/conf/apps/utils.py
2017-02-26 13:36:36.056990 INFO Loading Object utils using class utils from module utils
[2017-02-26 13:36:36.066024] INFO utils.initialize line 8 how bow dah
You’ll see the last line is different. You can customize this to your heart’s content. If you’re trying to discern your logs from those of AppDaemon
, you could even add some flair to the timestamp like '===> {t} {lvl} {name}.{mod} line {ln} {msg}'
. Really, it’s whatever you want.
You should be aware, if you want to use this, I would highly advise you to follow AppDaemon best-practices. Let is reside as an app which you have all other apps depend on, and then get the app and assign it to self.custom_logger
.
Why should you do this? Because it will help others to understand that you have a custom logger, and this can then become necessary/unnecessary when you are seeking help on a [possibly] unrelated problem.
Hope this helps,