Python’s print doesn’t show the file name and line number of the source code, which results in a lot of unsourcable console logs that are messy and inconvenient to parse (sometimes you don’t know where to find it and comment it out when you don’t want it to print).

The LK-Logger module aims to solve this problem.

Demo code:

from lk_logger import lk
a = 1
b = 2
print(a + b)
lk.log(a + b)
Copy the code

The print result is as follows:

It can be seen that lK-Logger has the following characteristics:

  1. The call method is very clean and concise, requiring no additional arguments
  2. You can print out the file name and line of the source code, and in Pycharm you can click to jump to the source code
  3. Also print out the name of the function/method where the source code is located (‘

    ‘ if it’s code at the top of the module)
  4. In addition, there is a more common method for LK-loggers:lk.loga(a + b)It can be printed at the same timeThe name of each variable(As shown below)

How to install

You can install via PIP:

pip install lk-logger
Copy the code

The LK-Logger module is only 12KB in size (version 3.6.3) without any third party dependencies.

Matters needing attention

  1. Lk-logger only supports Python 3.8 or later! The LK-Logger uses the walrus operator, so running in Python 3.7 will cause an error
  2. Lk-logger causes significant performance degradation, and it does a lot more work than Print. If you are concerned about the efficiency of your application and include a large number of printed logs in your application, then THE LK-Logger will not meet your needs
  3. If the parameter containslambda.*args.**kwargs, or in the case of a newline, the variable name in output cannot be fully displayed

Usage,

In addition to the most common methods demonstrated at the beginning, LK-Logger supports a richer set of options.

Import statements

The import statement of LK-Logger is fixed as from Lk_LOGGER import LK. Lk is A globally unique instance, so you import the same LK in module A and module B.

Print the element

The information printed by LK-Logger consists of the following elements:

  1. Source line: the format is{filepath}:{lineno}Filepath is the relative path and lineno is the line number (counting from 1), which is automatically recognized as a blue link that can be clicked to jump to in Pycharm
  2. Owning function: The name of the immediate function or method where the source is located, or ‘

    ‘ if it is at the top of the module.
  3. Serial number: This parameter is displayed when this parameter is enabled. The serial number can be ‘[1]’, ‘[2]’, ‘[3]’… Format, also supports ‘[1/10]’, ‘[2/10]’, ‘[3/10]’,… This format is usually used in loop structures
  4. Splitter line: displayed when this parameter is enabled. Secant supports defining strings (such as a string of short dashes, diamonds, squares, etc.) and lengths
  5. Variable name: Displayed when this parameter is enabled. The literal value of the variable is displayed at the source. Also note that if the argument to print is a string, for examplelk.loga('hello'), the variable name is not displayed
  6. Print value: the computed result of data or expression

Printing method

The main method of lK-logger is designed as log*, which is prefixed with log and suffixes as follows:

methods meaning Print the element
lk.log Log Source line, function, print value
lk.loga Log advanced Source line, function, variable name, print value
lk.logd Log with divider line Source line, function, partition line, variable name, print value
lk.logt Log with tag Source line, owning function, log level, variable name, print value
lk.logx Log with index Source line, function, serial number, print value
lk.logax Log advanced with index Source line, function, serial number, variable name, print value
lk.logdx Log with divider line and index Source line, function, serial number, secant line, print value
lk.logtx Log with tag and index Source line, function, log level, serial number, print value
lk.logdtx Log with divider line, tag and index Source line, function, log level, serial number, splitter line, print value

Other methods

  • Use lK.log_enabled = False to disable the printing function of LK (you can use this function before the main service is started to mask all log information).

  • You can use lk.print_important_messages() to collect statistics of all lK.logt records generated during running. You can learn how many INFO/WARNING/CRITICAL events occurred

  • All log-starting methods take an h argument that specifies the level of reflection. Such as:

    from lk_logger import lk
    
    def aaa() :
        bbb()
        
    def bbb() :
        ccc()
        
    def ccc() :
        ddd()
        
    def ddd() :
        a = 1
        b = 2
        lk.loga(a + b)  # Print source line pointing to current position (equivalent to h='self')
        lk.loga(a + b, h='parent')  The printed source line points to line 10
        lk.loga(a + b, h='grand_parent')  The printed source line points to line 7
        lk.loga(a + b, h='great_grand_parent')  The printed source line points to line 4.# do not specify a higher level. If mandatory, the numbers are passed: h = 7, 8, 9, 10...
    
    aaa()
    
    Copy the code

The principle of

The project is open source on Github repository: github.com/likianta/lk…

The LK-Logger uses the Pycharm console to print blue links

The core of LK-Logger uses the stack function of the INSPECT module to obtain the object stack, and then obtains the key information such as the file name, line number and function of the caller from the stack: