Skip to main content

function-burger🍔 is a library that outputs logs before and(or) after a function.

Project description

Test GitHub last commit (master) GitHub Repo stars
PyPI PyPI - Wheel Supported Python versions GitHub

function-burger🍔 Overview

function-burger🍔 is a library that outputs logs before and(or) after a function. Use it as a debugging tool.

example🍔 here!!
Open In Colab

Installation

Install with pip:

pip install function-burger

Usage

Simply specify the log output function as a decorator for the function you want to output logs.

  • burger(burger_log)

Logs are output before and after the function.

from function_burger import burger_log

@burger_log()
def example_func(input_str):
    print(f"input_str[{input_str}]")

example_func("example!!")

result:

[INFO] 2022-08-25 22:02:02.849391
input_str[example!!]
[INFO] 2022-08-25 22:02:02.852647
  • top(top_log)

Output log before function.

from function_burger import top_log

@top_log()
def example_func(input_str):
    print(f"input_str[{input_str}]")

example_func("example!!")

result:

[INFO] 2022-08-25 22:02:02.849391
input_str[example!!]
  • bottom(bottom_log)

Logs are output after the function.

from function_burger import bottom_log

@bottom_log()
def example_func(input_str):
    print(f"input_str[{input_str}]")

example_func("example!!")

result:

input_str[example!!]
[INFO] 2022-08-25 22:02:02.852647

Options

Each option is described below. The code example describes only the decorator portion and omits the logging function definition.

option table:

option_name burger_log top_log bottom_log
level
timestamp
elapsed_time
fname
tid
inputval
inputval_func
retval
retval_func
top_word *1
bottom_word *2
color

○:Available

*1 For top_log, specify word. *2 For bottom_log, specify word.

level

Specify the log level.

  • LogLevel.DEBUG
  • LogLevel.INFO(default)
  • LogLevel.WARN
  • LogLevel.ERROR
  • LogLevel.VERBOSE

Each log level is color-coded.

loglevel_color

example:

@burger_log()
# [INFO] 2022-08-25 22:02:02.849391

@burger_log(level=LogLevel.WARN)
# [WARN] 2022-08-25 22:02:02.849391

timestamp

Specify timestamp output.

  • True(default)
  • False

example:

@burger_log()
# [INFO] 2022-08-25 22:02:02.849391

@burger_log(timestamp=False)
# [INFO]

elapsed_time

Specify elapsed time output.

  • True
  • False(default)

example:

@burger_log(elapsed_time=True)
# [INFO] 2022-08-25 22:02:02.849391 elapsed time[0:00:00.000010]

fname

Specifies the output of the function name.

  • True
  • False(default)

example:

@burger_log(fname=True)
# [INFO] 2022-08-25 22:02:02.849391 func[function_name]

tid

Specifies the output of the thread ID.

  • True
  • False(default)

example:

@burger_log(tid=True)
# [INFO] 2022-08-25 22:02:02.849391 [thread_id]

inputval

Specifies the output of the input value.

  • True
  • False(default)

example:

@burger_log(inputval=True)
def func(a: int, b: int, *, c: int, d: int):
    pass

func(1, 2, c=3, d=4)
# [INFO] 2022-08-25 22:02:02.849391 args[(1, 2)] keywords[{'c': 3, 'd': 4}]

inputval_func

Specifies a function to edit the output format of input values. Used when the input value is an object.

  • None(default)
  • function or lambda-expression

example:

class ExampleCls:
    def __init__(self):
        self.num = 0
    def inc_num(self):
        self.num += 1
    def get_num(self):
        return self.num

@burger_log(
    inputval=True, inputval_func=lambda *a, **k: f"num[{a[0].get_num()}]"
)
def increment_num(cls):
    cls.inc_num()

cls = ExampleCls()
cls.inc_num()
cls.inc_num()
increment_num(cls)
# [INFO] 2022-08-25 22:02:02.849391 args[num[2]]

If @burger_log(inputval=True) is specified for the function:

[INFO] 2022-08-25 22:02:02.849391 args[(<__main__.ExampleCls object at 0x1046cd990>,)] keywords[{}]

retval

Specifies the output of the return value.

  • True
  • False(default)

example:

@burger_log(retval=True)
# [INFO] 2022-08-25 22:02:02.849391 ret[ret_val]

retval_func

Specifies a function to edit the output format of the return value. Used when the return value is an object.

  • None(default)
  • function or lambda-expression

example:

class ExampleCls:
    def __init__(self):
        self.num = 0
    def inc_num(self):
        self.num += 1
    def get_num(self):
        return self.num

@burger_log(retval=True, retval_func=lambda a: f"num[{a.get_num()}]")
def get_example():
    return ExampleCls()

cls = get_example()
# [INFO] 2022-08-25 22:02:02.849391 ret[num[0]]

If @burger_log(retval=True) is specified for the function:

[INFO] 2022-08-25 22:02:02.849391 ret[<__main__.ExampleCls object at 0x105a1d810>]

top_word

Specifies the string to be output to the log before the function call.

  • ""(default)
  • any character string

example:

@burger_log(top_word="start")
# [INFO] 2022-08-25 22:02:02.849391 start

If top_log, specify word instead of top_word.

@top_log(word="start")
# [INFO] 2022-08-25 22:02:02.849391 start

bottom_word

Specifies the string to be output to the log after a function call.

  • ""(default)
  • any character string

example:

@burger_log(bottom_word="end")
# [INFO] 2022-08-25 22:02:02.849491 end

If bottom_log, specify word instead of bottom_word.

@bottom_log(word="end")
# [INFO] 2022-08-25 22:02:02.849391 end

color

Specifies the text color of the log.

  • LogColor.VANILLA_SHAKE(default)
  • LogColor.MUSTARD
  • LogColor.KETCHUP
  • LogColor.MINT_CHOCOLATE
  • LogColor.SODA
  • LogColor.LETTUCE
  • LogColor.GRAPE_JUICE
  • LogColor.COLA

log_color

example:

@burger_log(color=LogColor.KETCHUP)
# Output contents are omitted. See image above for colors.

When an exception occurs

When an exception occurs, a message is forced to be displayed.

@burger_log()
def example():
    raise Exception("An exception occurred.")

example()

exception

License

This project is licensed under the terms of the MIT license.

Project details


Download files

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

Source Distribution

function-burger-1.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

function_burger-1.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file function-burger-1.1.0.tar.gz.

File metadata

  • Download URL: function-burger-1.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for function-burger-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fd99f6e5f3a79540abbd3c461554d1ac7512c691aa5ac13fa5a060995e004d22
MD5 98c76442fe1b733a7c842a0967cc7fa2
BLAKE2b-256 9b2668b8680f4d3dc692c936bcfb318c98eeb463e56706191776b2043eb089e6

See more details on using hashes here.

File details

Details for the file function_burger-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for function_burger-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6ec2b7fe5594262fc6b7dc50d82b246c01830e990a069ce2490d52e75b24e30
MD5 8d931f5c82a5e125197e87e3ca63bc37
BLAKE2b-256 d7929b1fd5e21c250127ff5e9d844d45b332ffd219eb563109b2fba03bf8cdb8

See more details on using hashes here.

Supported by

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