Giter Club home page Giter Club logo

logdecorator's People

Contributors

sighalt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

logdecorator's Issues

Log on specific result

Hi,
we want to add some log messages if a method returns a specific result (like False or None). I couldn't find a suitable decorator to archive this behaviour.

Are there any plans to add a decorator like described above to your project?

Or maybe you can explain how to extend your code to write my own decorator, based on your code?

Specify handler for auto-created logger

From https://github.com/sighalt/logdecorator#documentation

An alternative logger object. If no logger is given logdecorator creates a logger object with the name of the module the decorated function is in (decorated_function.__module__) Default: Creates a new logger with the name decorated_function.__module__

I find that the auto-created logger with the name of the decorated function is very useful. It might help to be able to specify a handler for this auto-created logger, e.g.

# app.py
import logging
logging.basicConfig(level=logging.INFO)

# Set up pyzmq log handler
from zmq.log.handlers import PUBHandler
ctx = zmq.Context.instance()
publisher = ctx.socket(zmq.PUB)
publisher.bind('ipc://log')
zmq_log_handler = PUBHandler(publisher)

# Set up logdecorator
from logdecorator import log_on_start

@log_on_start(logging.INFO, "Starting greeter", handler=zmq_log_handler)
def greeter():
    print("Hello world")

greeter()

Then, on a separate process, I would first run the following to listen for a log event:

$ python -m zmq.log ipc://log

Again, on a separate process, I run the main script:

$ python app.py

References

Please consider typing this useful package

Starting to use mypy --strict which disallows usage of this decorator package since it is untyped. If I had more time I'd fork it and type it for you. So, it is just a wishlist item for those of us whose projects need or want to pass mypy --strict type checking. Great package, thanks!

This would be a useful resource if you decide to undertake this: Typing decorators

Provide own/custom logger object

I've my own configuration of logger, that writes to FileHandler & StreamHandler, has custom formatting and etc..

I would like to use it.

How this is achievable with this library?

TypeError: catching classes that do not inherit from BaseException is not allowed

Hi,

there seems to be a mistake in your documentation, if i try to log errors like this:

@log_on_error(logging.ERROR, "Error on downloading",
              on_exceptions=[MyException1, MyException2],
              reraise=True)

i receive the following error:
TypeError: catching classes that do not inherit from BaseException is not allowed

this is because you try to catch the exceptions like defined in on_exceptions but the Python Docs (*) state the following:

An except clause may name multiple exceptions as a parenthesized tuple, for example:

so we must use the decorator like this:

@log_on_error(logging.ERROR, "Error on downloading",
              on_exceptions=(MyException1, MyException2),
              reraise=True)

Placeholder for the name of the decorated callable

Is it possible to log the decorated function name just like in args or return value?
ie. from this to

@log_on_start(logging.DEBUG, "Starting process_text_files with {file_name:s}")
def process_text_files(file_name=None):
   pass

something like this,

@log_on_start(logging.DEBUG, "Starting {_function:s} with {file_name:s}")
def process_text_files(file_name=None):
   pass

log_exception doesn't want to log on level CRITICAL/FATAL

Hello,
the log_exception decorator tries to force me to log on level ERROR instead of CRITICAL/FATAL or any self defined level, if i use one of those levels i get the following warning:
UserWarning: `log_exception` can only log into ERROR log level
What's the intention behind this warning? Why is this decorator bound to this specific log level?

Using data accessible via 'self' in messages for decorated methods

It would be nice to be able to have access in messages to data sitting on self, if the decorated function is a method.

For instance, I use logdecorator with my SshClient class (a simple wrapper for some features of paramiko and scp). So I use log_on_start on my connect() method and the message is a simple:

"Establishing connection".

But if I had access to host and port that are sitting unused on self, the message would be a more descriptive:

"Establishing connection to {host}:{port}".

Access self in logging of instance methods

Nice decorator lib. Thanks for open sourcing it.

I can log messages involving self if I return self in the instance method:

def batz(self) -> FooBar:
    ...
   return self

and then do, say:

@log_on_end(log_level=logging.DEBUG, "self.foo: {self.foo}", logger=logger, result_format_variable="self")

but it would be nice if I could access self without returning self as some instance methods should not have a return value, or more specifically should return None. I.e., It would be cool to use logdecorator to log instance var state without having to add a return of self to accomplish it.

I looked at the code and perhaps it could be done, but I'd have to look into it further. I guess it could be a feature request if it isn't already possible in some better way that I have not yet discovered.

Decorators hide actual type hints

Hi! First of all, thanks for the great project. I've been using it since recently and find it a great convenience.

Unfortunately I noticed that the decorators hide the type hints of the decorated functions, making mypy complain about mismatching types. For example:

@log_on_end(...)
def foo() -> bool:
  return True

Results in mypy reporting the returned type as Any, while the actual type is bool. This can be solved by using PEP-612.

Is there any chance you could update your type hints to use PEP-612 for the next release in order to preserve the original types?

Asyncio compatibility

Logdecorator isn't asyncio compatible, the logs get fired before the coroutine is awaited.

See this example:

In [9]: @log_on_start(logging.CRITICAL, "START") 
   ...: @log_on_end(logging.CRITICAL, "END") 
   ...: async def test(): 
   ...:     print("test") 
   ...:                                                                                                                                                  

In [10]:                                                                                                                                                 

In [10]: await test()                                                                                                                                    
START
END
test

Expected Output was:

START
test
END

Its not even necessary to await the coroutine, see:

In [14]: test()                                                                                                                                          
START
END
Out[14]: <coroutine object test at 0x7f14c7cefc48>

log_on_error and log_exception should reraise by default

Hi and thanks for this awesome project,
i think you should consider making log_on_error and log_exception decorators reraise exceptions by default.
Otherwise adding those decorators would change my existing program flow (of course i can explicit set reraise to true, but in my opinion the logging part should not per default affect the flow of my program).

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.