Code Monkey home page Code Monkey logo

asynclogger's Introduction

dependency


  • structlog==21.2.0
  • aiologger==0.7.0
  • graypy==2.1.0

How does work?

Schema

@startuml
Client -> structlog : logger.debug(Message)
structlog -> asynclogger.Adapter : structlog.BoundLogger._proxy_to_logger()
asynclogger.Adapter -> aiologger : aiologger.debug()
aiologger -> asynclogger.ConsoleHandler : aiologger.AsyncStreamHandler
aiologger -> asyncloggerGraylogHandler : aiologger.Handler
@enduml

How to use?

Setting variables

.env

LOGGER_CONSOLE=True  # Enable console output
LOGGER_CONSOLE_LEVEL=DEBUG # Level of displayed logs

LOGGER_GRAYLOG=True # Enable output in Graylog
LOGGER_GRAYLOG_LEVEL=DEBUG
LOGGER_GRAYLOG_HOST=localhost
LOGGER_GRAYLOG_PORT=12201

Connecting in project

from asynclogger import add_handler
from asynclogger import console_handler
from asynclogger import get_logger
from asynclogger import graylog_handler

from ... import config

if config.LOGGER_CONSOLE:
    add_handler(console_handler(level=config.LOGGER_CONSOLE_LEVEL))

if config.LOGGER_GRAYLOG:
    add_handler(
        graylog_handler(
            host=config.LOGGER_GRAYLOG_HOST,
            port=config.LOGGER_GRAYLOG_PORT,
            level=config.LOGGER_GRAYLOG_LEVEL,
        )
    )


logger = get_logger()

await logger.debug("Debug")
>>> 2023-06-09T08:36:20.601568 DEBUG: Debug

feature Field

from asynclogger.fields import Field

logger = get_logger()

await logger.debug("Debug", Field.levelname("DEBUG"))
>>> 2023-06-09T08:44:07.261025 DEBUG: Debug levelname=DEBUG

Displaying specific fields

from asynclogger.fields import Field

add_handler(
        console_handler(
            level=LOGGER_CONSOLE_LEVEL,
            fields=[Field.levelname, Field.timestamp, Field.event, 'include']
        )
    )

logger = get_logger()

await logger.debug("Debug", include="Include Field", other="Other")
>>> 2023-06-09T08:42:14.447572 DEBUG: Debug include=Invclude Field

Setting the order, format, and color scheme for display

Priority override

from asynclogger.fields import FIELDS_SETTINGS, Field

await logger.debug("Debug")
 >>> 2023-06-09T08:42:14.447572 DEBUG: Debug
 
FIELDS_SETTINGS[Field.levelname]["priority"] = 0
FIELDS_SETTINGS[Field.timestamp]["priority"] = 1

await logger.debug("Debug")
 >>> DEBUG: 2023-06-09T08:42:14.447572 Debug

Format override

from asynclogger.fields import FIELDS_SETTINGS, Field

await logger.debug("Debug")
 >>> 2023-06-09T08:42:14.447572 DEBUG: Debug
 
FIELDS_SETTINGS[Field.LevelName] = dict(
    priority=1,
    template=lambda method, key, value: f"{structlog.dev.BRIGHT} || {value} ||"
)

await logger.debug("Debug")
 >>> 2023-06-09T08:42:14.447572 || DEBUG || Debug

asynclogger's People

Contributors

alexdemure avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

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.