Skip to main content

logging helper for functions and methods

Project description

log4func

Software Version MIT License

Japanese Page

Overview

Log helpers for functions and methods.

License

MIT License

Installation

pip

pip install log4func

github

pip install git+https://github.com/bugph0bia/log4func.git

Usage

@log_start_end

Decorator that logs out the start and end of the target function.
Log output using the function passed as argument.

Parameter

  • logging_func (Callable): Log output function, must be able to accept an argument of type str and log it. Can be print or logging module's debug, info, and more.
  • with_start (bool): Toggles whether or not the start log is output. Default is True.
  • with_end (bool): Toggles whether or not the end log is output. Default is True.

Example

@log_start_end(print)
def foo(a, b):
    print(f'calc {a} + {b}')
    return a + b

foo(1, 2)

Output

foo start
calc 1 + 2
foo end

@log_args_return

Decorator that logs real arguments and return values of the target function.
Log output using the function passed as argument.

Parameter

  • logging_func (Callable): Log output function, must be able to accept an argument of type str and log it. Can be print or logging module's debug, info, and more.
  • with_args (bool): Toggles whether or not the arguments log is output. Default is True.
  • with_return (bool): Toggles whether or not the return log is output. Default is True.
  • oneline (bool): Toggles whether or not logs are combined into a single line. Default is False.

Example

@log_args_return(print)
def foo(a, b):
    print(f'calc {a} + {b}')
    return a + b

foo(1, b=2)

Output

foo args:
  1
  b=2
calc 1 + 2
foo return:
  3
@log_args_return(print, oneline=True)
def foo(a, b):
    print(f'calc {a} + {b}')
    return a + b

foo(1, b=2)

Output

foo args: 1, b=2
calc 1 + 2
foo return: 3

@log_traceback

Decorator that logs out a traceback of exceptions raised by the target function.
Log output using the function passed as argument.

Traceback is output to the screen by default, but it is useful if you want to keep it in a file. (e.g. in the logging module)

Parameter

  • logging_func (Callable): Log output function, must be able to accept an argument of type str and log it. Can be print or logging module's debug, info, and more.

Note

  • The part that log4func.log_traceback wraps is also output to traceback.

Example

@log_traceback(print)
def foo(a, b):
    return a / b

foo(1, 0)

Output

Traceback (most recent call last):
File "...\log4func.py", line 184, in wrapper
    decorated_func(*args, **kwargs)
File "...\test.py", line 2, in foo
    a / b
ZeroDivisionError: division by zero

@wraps_logging_params

Decorator that allows the original function name to be output when the function name is logged in the decorator.

Supplemental Explanation

There is a problem that if you apply your own decorator to a function, the function name of the decorator becomes the function name of the original function when you get the original function name with func.__name__. To solve this problem, you can apply @functool.wraps decorator to your own decorator.

Similarly, if you apply your own decorator to a function, the function name (%(funcName)s) output inside the original function using the logging module will be the decorator's function name. To solve this problem, you can apply @log4func.wraps_logging_params decorator to your own decorator.

Note

  • If used with @classmethod or @staticmethod, it must be an inner decorator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

log4func-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file log4func-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: log4func-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for log4func-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6dfe154ff81d9fdf2089373f54dcc9dda6b09abf06a186967f47b4f477a9b632
MD5 85226cc84e0b90791cd643f0982b88f7
BLAKE2b-256 bea069a80e89728a34ebce45ca33b6724ccd69545a41d8326a98f71d84faa4ea

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page