Giter Club home page Giter Club logo

Comments (7)

yawor avatar yawor commented on July 26, 2024

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.

turboc1208 avatar turboc1208 commented on July 26, 2024

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.

yawor avatar yawor commented on July 26, 2024

@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.

yawor avatar yawor commented on July 26, 2024

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.

turboc1208 avatar turboc1208 commented on July 26, 2024

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.

yawor avatar yawor commented on July 26, 2024

@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.

acockburn avatar acockburn commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.