Skip to main content

json based python logger with support for slack notification

Project description

Installation

Install the package fasal-logger using the following command

pip install --upgrade fasal-logger

Configuration

  • Create a file logger.yml. Copy the contents from the repository and make the necessary changes (If needed)
version: 1
disable_existing_loggers: no
formatters:
  simple:
    format: "%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s"
    datefmt: '%Y-%m-%d %H:%M:%S'
  fasalFormat:
    (): fasal_logger.FasalStdlibFormatter
    datefmt: '%Y-%m-%d %H:%M:%S'
    extra: {"author": "${USER}"}
    exclude_fields: [ecs, log.original]

handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: fasalFormat
    stream: ext://sys.stdout
  file:
    class : logging.handlers.RotatingFileHandler
    level: DEBUG
    formatter: fasalFormat
    filename: 'logging.example.log'
    mode: a
    maxBytes: 1000000  # 1 MB
    backupCount: 2
    encoding: utf8

root:
  level: DEBUG
  handlers: [console]
  propogate: no

loggers:
  fasalLogger:
    level: DEBUG
    handlers: [console]
    propagate: no
  • The logger.yml defined here defaults to console. In order to log the contents into a file, make the required changes to the logger.yml.
root:
  handlers: [console, file]

loggers:
  fasalLogger:
    handlers: [console, file]

And change the filename and location.

handler:
    file:
        filename: 'logging.example.log'

Usage

In order to use the logger in your code, add the following piece of code at the top of your .py file

Note: Get the webhook from the infra team to able to send messages to slack to any other channel. (By default using #fasal-ai-infra)

import logging
import logging.config
import os
from fasal_logger import LoggerInitializer, SlackNotification

logging.captureWarnings(True)
logger = logging.getLogger(__name__)
logger_init = LoggerInitializer()
logger_init(logger=logger, config='path_to_logger.yml')
slk = SlackNotification() # set parameter for webhook, DEV (if needed)

# Use logger now
logger.info("Logger set")

# Send a message to slack channel
slk.notify(message="testing")

Variables taken from environment are:

Set these variables as required in the file used to read environment variable.

Note: You can keep env variables in an yaml file local.yml and use python-dotenv-yaml library to read it

  • SLACK_WEBHOOK: Channel webhook trigger, defaults to slack channel #fasal-ai-infra
  • DEV: If True, no message is send to slack
  • ENV: logger environment (staging/production/development)

Another example usage:

import logging
import logging.config
import os

from functools import reduce
from fasal_logger import LoggerInitializer, SlackNotification

logging.captureWarnings(True)
logger = logging.getLogger(__name__)
logger_init = LoggerInitializer()
logger_init(logger=logger, config='logger.yml')
slk = SlackNotification(DEV=True)
high_level_stuff = {'custom_tag': 'tagged-at-line'}


logger.info("Starting the function", extra={'tag': 'xxx'})
def my_sum(a,b):
    if isinstance(a, str):
        logger.warning("This is not int")
    logger.info(f'performing addition of {a}, {b}')
    try:
        result = a+b
        logger.info(f'result: {result}')
    except Exception as err:
        logger.error(err)
        raise err
    return result

def run_request():
    import requests
    logger.info("request")
    ploads = {'things':2,'total':25}
    r = requests.get('https://httpbin.org/get',params=ploads)


param = [1,2,3]
result = reduce(my_sum, param)
logger.info(f"final: {result}", extra=high_level_stuff)
run_request()
slk.notify(message="this is test")

Note:

  1. Use logger.info() with first argument as string
  2. In order to pass another set of key: value pairs to logger.info / logger.warnings / logger.debug
  • Pass the key: value pair to logger.yaml
formatters:
fasalFormat:
  (): fasal_logger.FasalStdlibFormatter
  datefmt: '%Y-%m-%d %H:%M:%S'
  extra: {"author": "${USER}", "key-1": "value-1", "key-2": "value-2"}
  • Pass a dictionary to argument extra inside logger.info(). However, doing so will only effect the logger's log where its added and adding in logger.yaml will effect it globally
logger.info("my message", extra = {'key-1': 'value-1'})

Build and Published using (poetry)[https://python-poetry.org/docs/cli/#publish]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fasal_logger-0.0.5.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file fasal_logger-0.0.5.tar.gz.

File metadata

  • Download URL: fasal_logger-0.0.5.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for fasal_logger-0.0.5.tar.gz
Algorithm Hash digest
SHA256 92d82addaaa4c9539a94f0fb2968174def430ccbc2482039a2fa0838bb6ad1c7
MD5 bd5a489c12c81dd0d47eee03b3cb4ed0
BLAKE2b-256 1e69b51084c851755a9f679b251b93f0b181ebb98273eda8d44835efc0da1bc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page