Python日志模块

 

gaogaotiantian/objprint: A library that can print Python objects in human readable format (github.com)

objprint

A library that can print Python objects in human readable format

 

 

 

Delgan/loguru: Python logging made (stupidly) simple (github.com)

Loguru is a library which aims to bring enjoyable logging in Python.

Did you ever feel lazy about configuring a logger and used print() instead?... I did, yet logging is fundamental to every application and eases the process of debugging. Using Loguru you have no excuse not to use logging from the start, this is as simple as from loguru import logger.

Also, this library is intended to make Python logging less painful by adding a bunch of useful functionalities that solve caveats of the standard loggers. Using logs in your application should be an automatism, Loguru tries to make it both pleasant and powerful.

 

 

Python新式格式化输出:f-string

 Adding a way to perform automatic logging of variables "a la icecream"? [feature request] · Issue #541 · Delgan/loguru (github.com)

I haven't looked closely yet at how IceCream could be incorporated with Loguru, but while reading your question I thought you could maybe use the f-string debugging feature from Python 3.8?

>>> some_var = 42
>>> logger.info(f"{some_var=}")
2021-12-05 17:50:53.973 | INFO     | __main__:<module>:1 - some_var=42
 
 

 

 

gruns/icecream: 🍦 Never use print() to debug again. (github.com)

IceCream — Never use print() to debug again

Do you ever use print() or log() to debug your code? Of course you do. IceCream, or ic for short, makes print debugging a little sweeter.

ic() is like print(), but better:

  1. It prints both expressions/variable names and their values.
  2. It's 60% faster to type.
  3. Data structures are pretty printed.
  4. Output is syntax highlighted.
  5. It optionally includes program context: filename, line number, and parent function.

IceCream is well tested, permissively licensed, and supports Python 2, Python 3, PyPy2, and PyPy3.

Inspect Variables

Have you ever printed variables or expressions to debug your program? If you've ever typed something like

print(foo('123'))

or the more thorough

print("foo('123')", foo('123'))

then ic() will put a smile on your face. With arguments, ic() inspects itself and prints both its own arguments and the values of those arguments.

from icecream import ic

def foo(i):
    return i + 333

ic(foo(123))

Prints

ic| foo(123): 456

Similarly,

d = {'key': {1: 'one'}}
ic(d['key'][1])

class klass():
    attr = 'yep'
ic(klass.attr)

Prints

ic| d['key'][1]: 'one'
ic| klass.attr: 'yep'
 

Just give ic() a variable or expression and you're done. Easy.

 

 

 

 

pprint — Data pretty printer — Python 3.11.4 documentation

pprint — Data pretty printer

Source code: Lib/pprint.py


The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets or classes are included, as well as many other objects which are not representable as Python literals.

The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width. Construct PrettyPrinter objects explicitly if you need to adjust the width constraint.

 

 

 

posted @ 2023-07-25 11:48  sinferwu  阅读(8)  评论(0编辑  收藏  举报