Comments (7)
This is not a macro or a preprocessor. It's a simple string replacement with values taken from Python's call frame stack.
If you want to change a default behaviour of log
and error
methods there should be no problem in overwriting them in your app's class:
class MyApp(AppDaemon):
def log(self, msg, level='INFO'):
msg = self._sub_stack("__function__ (__line__) {}".format(msg))
ha.log(self._logger, level, msg, self.name)
def error(self, msg, level="WARNING"):
msg = self._sub_stack("__function__ (__line__) {}".format(msg))
ha.log(self._error, level, msg, self.name)
That way when you use anywhere else in your class self.log('foo')
or self.error('foo')
the function and line number will be automatically added. When you no longer need func and line no then just delete or comment out overwritten methods.
from appdaemon.
The string replacement is being done inside the AppDaemon libraries. This is an example of a place where it could be helpful. As I said in the message, continually typing all of the text to get the function and line number is a pain, it pushes the text further over in the code which makes it harder to read. The problem with overriding it is that then there is another item on the stack that suddenly becomes the function that is getting it's name and line number put in for every log message. The alternative is to not use the log function in AD and to write one of my own that gets the function and line number from inspect. I don't like this option either because it doesn't use standard functionality of AD.
from appdaemon.
@turboc1208 have you actually tried the code I've posted in my previous comment? Overwriting a method doesn't add anything on the call stack during method call. I've used the same code that is in the base class. The only different is that the log message is being augmented with function and line number placeholders automatically.
As it is currently done in the AD, there's no other way to do that automatically and still use the built-in feature like you've said. But as long as you keep your call stack at the exact length even your own log function will work (you need to call self._sub_stack
to replace placeholders directly from your log function).
To be honest, I don't really like how it is done and I'm going to prepare a PR which uses a log formatters instead of string replacement. There won't be any need to include something in the message itself.
from appdaemon.
As I've mentioned in the previous comment, I've created a PR #40 which removes the need for including __module__
, __line__
and __function__
placeholders and moves this info to a format string.
from appdaemon.
I did not use your code, because I did that a couple of weeks ago, and have moved past it as not being the solution I was looking for. The module, line and function were put in to make the inclusion of the module, line and function optional based on the original request to have them included in the log messages. My first request was simply to have them included. How they are included I really don't care, but given the option, I would choose less typing.
from appdaemon.
@turboc1208 then you're welcome to test my PR if it meets your needs. Default format string doesn't show these fields but PR description shows how to add them if needed.
from appdaemon.
I'm closing this - the original request for a preprocessor for Python code just just not how Python works, there are better ways to solve these types of problems.
from appdaemon.
Related Issues (20)
- Documentation to point to new configuration location HOT 5
- Weirt example in `Choosing a Start Time`
- Issue logging DEBUG level log entries HOT 2
- Add support for service calls that return data such as todo/get_items HOT 3
- Logging (self.log) does not properly handle encoding of some non-English characters. HOT 1
- Schedy App/script not working after update Appdaemon add-on to 0.15.2
- Add ability to trigger a manual check_app_update or to set a custom interval (or: is utility_delay safe to change?)
- Throw an error / refuse to load if apps.yaml has a duplicate top-level key HOT 2
- Double reset_timer set execution time to first time set
- Python 3.12 support HOT 4
- Python package imports - "expert mode" HOT 1
- Warning about "secrtets" from apps where yaml is commented out
- listen_log never receives DEBUG messages HOT 1
- get_history error - takes one positional arguement but 2 were given HOT 2
- Secrets are viewable in plaintext in AppDaemon dashboard via app arguments HOT 1
- appdaemon:dev container stop to work after today update HOT 3
- mqtt retained messages not send to app on appdaemon startup HOT 1
- Support `silent` in `cancel_listen_state` HOT 2
- Cannot call service that returns a response
- Some apps not reloaded after HA restart
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from appdaemon.