function-burger

function-burgerπŸ” is a library that outputs logs before and(or) after a function.


License
MIT
Install
pip install function-burger==1.1.0

Documentation

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.