以简单的方式登录json!

noodle-logging的Python项目详细描述


面条日志-python 3.x的小json日志

Noodle Logging是一个很小的包,旨在使从Python应用程序到JSON的日志记录变得更容易。

依赖关系

  1. structlog
  2. python-json-logger
  3. six

安装

pip3 install noodle-logging

用法示例

1.基本日志记录
fromnoodle_logging.loggerimportlogdeftest():log.info("event",some_parameter=12,some_other_parameter="hello")if__name__=="__main__":test()
输出
{"message":"event","some_parameter":12,"some_other_parameter":"hello","logger":"__main__","level":"info","timestamp":"2019-05-28T11:59:14.975823Z"}
2。高级配置-使用装饰器
importtimefromnoodle_logging.decoratorsimportprofile@profiledefsome_method(*args,**kwargs):time.sleep(1)print(some_method.__name__,"method in execution with args ",args," and kwargs ",kwargs)time.sleep(1)if__name__=="__main__":some_method(1,2,3,a=2,b=3)
输出
{"message": "Function Profiler", "fn_name": "some_method", "fn_id": "2df1335aa93f41438412da6b517a6781", "timestamp_start": 1559044955.3319192, "fn_args": "1,2,3", "fn_kwargs": "{\"a\": 2, \"b\": 3}", "logger": "noodle_logging.decorators", "level": "info", "timestamp": "2019-05-28T12:02:35.332396Z"}
>>> some_method method in execution with args  (1, 2, 3)  and kwargs  {'a': 2, 'b': 3}
{"message": "Function Profiler", "fn_name": "some_method", "fn_id": "2df1335aa93f41438412da6b517a6781", "timestamp_start": 1559044955.3319192, "fn_args": "1,2,3", "fn_kwargs": "{\"a\": 2, \"b\": 3}", "timestamp_end": 1559044957.340735, "exec_time": 2.0088157653808594, "logger": "noodle_logging.decorators", "level": "info", "timestamp": "2019-05-28T12:02:37.340991Z"}
3。捕捉(并记录)异常-使用装饰符
fromnoodle_logging.decoratorsimportlog_exceptions@log_exceptionsdefsome_other_method(*args,**kwargs):print(some_other_method.__name__,"method in execution with args ",args," and kwargs ",kwargs)print(1/0)# Below line will not be executed.print("Execution completed",some_other_method.__name__)if__name__=="__main__":some_other_method(1,2,3,a=2,b=3,c=4)
输出
some_other_method method in execution with args  (1, 2, 3)  and kwargs  {'a': 2, 'b': 3, 'c': 4}
{"message": "Exception Found.", "exception": "Traceback (most recent call last):\n  File \"/Users/askar.ali/Desktop/noodle-logging-original/noodle_logging/decorators.py\", line 48, in wrapper\n    return func(*args, **kwargs)\n  File \"run.py\", line 29, in some_other_method\n    print(1/0)\nZeroDivisionError: division by zero", "fn_args": "1,2,3", "fn_kwargs": "{\"a\": 2, \"b\": 3, \"c\": 4}", "logger": "noodle_logging.decorators", "level": "error", "timestamp": "2019-05-28T12:06:10.188581Z"}
Traceback (most recent call last):
  File "run.py", line 49, in <module>
    some_other_method(1, 2, 3, a=2, b=3, c=4)
  File "/Users/askar.ali/Desktop/noodle-logging-original/noodle_logging/decorators.py", line 48, in wrapper
    return func(*args, **kwargs)
  File "run.py", line 29, in some_other_method
    print(1/0)
ZeroDivisionError: division by zero
4。分析+捕获(和记录)异常-使用装饰符
fromnoodle_logging.decoratorsimportprofile,log_exceptions@profile@log_exceptionsdefnoodling_around(*args,**kwargs):print(noodling_around.__name__,"method in execution with args ",args," and kwargs ",kwargs)print(1/0)# This line will not be executed.print("Execution completed",noodling_around.__name__)if__name__=="__main__":noodling_around(1,2,3,a=2,b=3,c=4)
输出
{"message": "Function Profiler", "fn_name": "noodling_around", "fn_id": "9a3e74924b77440c9a5f1fe4c05522e6", "timestamp_start": 1559045360.7205062, "fn_args": "1,2,3", "fn_kwargs": "{\"a\": 2, \"b\": 3, \"c\": 4}", "logger": "noodle_logging.decorators", "level": "info", "timestamp": "2019-05-28T12:09:20.720621Z"}
noodling_around method in execution with args  (1, 2, 3)  and kwargs  {'a': 2, 'b': 3, 'c': 4}
{"message": "Exception Found.", "exception": "Traceback (most recent call last):\n  File \"/Users/askar.ali/Desktop/noodle-logging-original/noodle_logging/decorators.py\", line 48, in wrapper\n    return func(*args, **kwargs)\n  File \"run.py\", line 41, in noodling_around\n    print(1 / 0)\nZeroDivisionError: division by zero", "fn_args": "1,2,3", "fn_kwargs": "{\"a\": 2, \"b\": 3, \"c\": 4}", "logger": "noodle_logging.decorators", "level": "error", "timestamp": "2019-05-28T12:09:20.721559Z"}
Traceback (most recent call last):
  File "run.py", line 50, in <module>
    noodling_around(1, 2, 3, a=2, b=3, c=4)
  File "/Users/askar.ali/Desktop/noodle-logging-original/noodle_logging/decorators.py", line 26, in wrapper
    result = func(*args, **kwargs)
  File "/Users/askar.ali/Desktop/noodle-logging-original/noodle_logging/decorators.py", line 48, in wrapper
    return func(*args, **kwargs)
  File "run.py", line 41, in noodling_around
    print(1 / 0)
ZeroDivisionError: division by zero

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JAVAexe jar文件读取txt文件,但内容在不同的计算机中都被破坏(显示外来语言..)   java BufferedReader提供缺少的字符   java我希望全局捕获所有可能的安卓异常,以便应用程序无法关闭   java MATLAB JA Builder   java如何将一副牌一分为二?   字典在Java中使用映射条目时出错   Java命令提示符仿真器   java HC06蓝牙模块   如何在java中获得与CallableStatement一起使用的完全限定SQL类型名。registerOutParameter   java如何为文件中的每一行整数创建数组   java从文本框传递参数   java如何将参数传递到工厂以创建对象?   java JSP无法执行脚本标记中包含的javascript函数