Giter VIP home page Giter VIP logo

magiclogger's Introduction

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       | }

magiclogger's People

Contributors

nicolasvo avatar rcourivaud avatar teamkudoztech avatar

Watchers

 avatar  avatar

Forkers

nicolasvo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.