Skip to content

✨ Simple magic Python API to handle logging easily ✨

License

Notifications You must be signed in to change notification settings

rcourivaud/MagicLogger

Repository files navigation

MagicLogger

Documentation Status Updates

Log handler to log better and send logs to Logstash

Features

pip install magic_logger

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Requirements

Enable your Logstash pipeline to listen to incoming JSON streams on port 5000

input {
    tcp {
        port => 5000
        codec => json
    }
}
output {
    elasticsearch {
        hosts => ["elasticsearch:9200"]
        index => "logstash_index"
        document_type => "logtype"
    }
stdout { codec => rubydebug }
}

How to use

  1. Import module
from magic_logger import MagicLogger
  1. Instantiate MagicLogger with a name. Logstash host is changed with host parameter. Define your own host in a constant file.
logger = MagicLogger('Name', host='localhost')
  1. Decorate a function in order to log it into Logstash and into the console
@logger.decorate('This is a message.')
def a(*args, **kwargs):
    return 'This is a return'

a('arg_1', 'arg_2', kwarg_1='hello', kwarg_2='world')

Terminal output:

2018-05-25 23:09:35,514 - Name - INFO - Connection to logstash successful.
2018-05-25 23:09:35,518 - Name - DEBUG - This is a message.

Logstash output:

logstash_1       | {
logstash_1       |          "stack_info" => nil,
logstash_1       |            "@version" => "1",
logstash_1       |                "type" => "logstash",
logstash_1       |             "message" => "This is a message.",
logstash_1       |     "function_kwargs" => {
logstash_1       |         "kwarg_2" => "world",
logstash_1       |         "kwarg_1" => "hello"
logstash_1       |     },
logstash_1       |                "host" => "Nicolass-MacBook-Pro.local",
logstash_1       |       "function_name" => "a",
logstash_1       |                "path" => "/Users/nico/corners/MagicLogger/magic_logger/magic_logger.py",
logstash_1       |               "class" => nil,
logstash_1       |                "port" => 51772,
logstash_1       |               "level" => "DEBUG",
logstash_1       |                "tags" => [],
logstash_1       |        "function_res" => "This is a return",
logstash_1       |          "@timestamp" => 2018-05-25T21:09:35.518Z,
logstash_1       |      "execution_time" => 5.0e-06,
logstash_1       |         "logger_name" => "Name",
logstash_1       |      "function_class" => nil
logstash_1       | }
  1. Add an extra to the decorator within the decorated function with the update_extra method
@logger.decorate('This is a message')
def a():
    logger.update_extra(post_extra='This is a new extra')
    return 'This is a return'

a()
  1. Write a regular log
test_list = [1, 2, 3]
test_string = "This is a string"
logger.info('This is a message', extra = {"a_list": test_list, "a_string": test_string})

Terminal output:

2018-05-25 17:08:15,654 - Name - INFO - This is a message

Logstash output:

logstash_1       | {
logstash_1       |      "@timestamp" => 2018-05-25T15:08:15.654Z,
logstash_1       |         "message" => "This is a message",
logstash_1       |            "type" => "logstash",
logstash_1       |      "stack_info" => nil,
logstash_1       |     "logger_name" => "Name",
logstash_1       |            "path" => "test.py",
logstash_1       |            "port" => 33542,
logstash_1       |        "@version" => "1",
logstash_1       |          "a_list" => [
logstash_1       |         [0] 1,
logstash_1       |         [1] 2,
logstash_1       |         [2] 3
logstash_1       |     ],
logstash_1       |        "a_string" => "This is a string",
logstash_1       |            "tags" => [],
logstash_1       |            "host" => "MBP-C02WC1F4HV2Q.local",
logstash_1       |           "level" => "INFO"
logstash_1       | }

About

✨ Simple magic Python API to handle logging easily ✨

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •