Skip to main content

Generic logs pattern builder for Python and Flask, built on top of logging module.

Project description

Motifer

Motifer is a generic logs pattern builder on top of Python logging. It covers multiple usecases as follows.

  • Log pattern validation.
  • Consistent log pattern across the application.
  • Logstash and Cloudtrail support.
  • Request and response logging with a unique request id for a request flow.

Installation

Motifer requires Python to run.

Install the dependencies and devDependencies and start the server.

$ pip3 install motifer

Usage

The recommended way to use motifer is to create a logger. The simplest way to do this is using LoggerFactory.

LoggerFactory

Initialize the LoggerFactory object once and use it in different Python files.

from motifer import LogFactory

factory = LogFactory(service="appname", log_level="debug")
logger = factory.initialize()
logger.debug("Debug message")

All log levels supported by Python logging are supported.

2023-02-20 23:05:25,876 [appname] [DEBUG] [test-logs.py:16] Debug message
2023-02-20 23:05:25,876 [appname] [INFO] [test-logs.py:17] Info message
2023-02-20 23:05:25,876 [appname] [WARN] [test-logs.py:18] Warning message
2023-02-20 23:05:25,876 [appname] [ERROR] [test-logs.py:19] Error message
2023-02-20 23:05:25,876 [appname] [CRITICAL] [test-logs.py:20] Critical message
2023-02-20 23:05:25,876 [appname] [ERROR] [test-logs.py:24] This is some exception
Traceback (most recent call last):
  File "test-logs.py", line 22, in main
    raise Exception("This is some exception")
Exception: This is some exception
2023-02-20 23:05:25,877 [appname] [DEBUG] [innerlayer.py:11] inner debug
2023-02-20 23:05:25,877 [appname] [INFO] [innerlayer.py:12] inner Info message
2023-02-20 23:05:25,877 [appname] [WARN] [innerlayer.py:13] inner Warning message
2023-02-20 23:05:25,877 [appname] [ERROR] [innerlayer.py:14] inner Error message
2023-02-20 23:05:25,877 [appname] [CRITICAL] [innerlayer.py:15] inner Critical message

FlaskLogFactory

Initialize the FlaskLogFactory object once with flask server object and use it in different routes.

index.py / app.py
from flask import Flask
from motifer import FlaskLogFactory

app = Flask(__name__)
factory = FlaskLogFactory(service="webappname", log_level="debug", server=app)
logger = factory.initialize()

def calculate():
    logger.error("Some error occured")
    return 10

@app.route('/')
def health():
    logger.debug("In the root route of sample app.")
    calculate()
    return {"status": "okay"}

if __name__ == '__main__':
    app.run()

Request id is of UUID V4 type.

2023-02-20 23:16:48,563 [webappname] [a6df150a-1fcd-4fb0-a915-a13d1dac24bd] [INFO] [flask_factory.py:40] [request] [GET] [127.0.0.1] [/] [ImmutableMultiDict([])]
2023-02-20 23:16:48,563 [webappname] [a6df150a-1fcd-4fb0-a915-a13d1dac24bd] [DEBUG] [flask-runner-copy.py:17] In the root route of sample app.
2023-02-20 23:16:48,564 [webappname] [a6df150a-1fcd-4fb0-a915-a13d1dac24bd] [ERROR] [flask-runner-copy.py:12] Some error occured
2023-02-20 23:16:48,565 [webappname] [a6df150a-1fcd-4fb0-a915-a13d1dac24bd] [INFO] [flask_factory.py:46] [response] [GET] [127.0.0.1] [/] [200] [18] [2] [Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36]
2023-02-20 23:17:16,299 [webappname] [f0bb5f15-40a2-4c66-93e0-6b7710021ae4] [INFO] [flask_factory.py:40] [request] [GET] [127.0.0.1] [/] [ImmutableMultiDict([])]
2023-02-20 23:17:16,300 [webappname] [f0bb5f15-40a2-4c66-93e0-6b7710021ae4] [DEBUG] [flask-runner-copy.py:17] In the root route of sample app.
2023-02-20 23:17:16,301 [webappname] [f0bb5f15-40a2-4c66-93e0-6b7710021ae4] [ERROR] [flask-runner-copy.py:12] Some error occured
2023-02-20 23:17:16,301 [webappname] [f0bb5f15-40a2-4c66-93e0-6b7710021ae4] [INFO] [flask_factory.py:46] [response] [GET] [127.0.0.1] [/] [200] [18] [3] [Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36]

Log Patterns

Request Logs
TIMESTAMP_ISO [request] [REQUEST_ID] [APP_NAME] [LOG_LEVEL] [REQUEST_METHOD] [REQUEST_IP] [API_PATH] [BODY]
Service Logs
TIMESTAMP_ISO [service] [REQUEST_ID] [APP_NAME] [LOG_LEVEL] [FILENAME] MULTI_OR_SINGLE_LINE_MESSAGE
Response Logs
TIMESTAMP_ISO [response] [REQUEST_ID] [APP_NAME] [LOG_LEVEL] [REQUEST_METHOD] [REQUEST_IP] [API_PATH] [RESPONSE_STATUS] [CONTENT_LENGTH] [RESPONSE_TIME] [USER_AGENT] 

LoggerFactory

The object has three parameter.

Param Description Mandatory Default Comments
service Application or service name. Yes NA This is a mandatory param.
level Log level for the application. No info Info is default log level.
options Array of objects for file appender and rotation. No null If not supplied file appender will not be attached.

FlaskLogFactory

The object has four parameter.

Param Description Mandatory Default Comments
service Application or service name. Yes NA This is a mandatory param.
level Log level for the application. Yes NA This is a mandatory param.
server Flask object Yes NA This is a mandatory param.
options Array of objects for file appender and rotation. No null If not supplied file appender will not be attached.

License

Apache 2.0

Free Software, Hell Yeah!

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

motifer-0.0.1.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file motifer-0.0.1.tar.gz.

File metadata

  • Download URL: motifer-0.0.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.9

File hashes

Hashes for motifer-0.0.1.tar.gz
Algorithm Hash digest
SHA256 12a7852b59bf4c4ce1f499714c8e0ccc22074ee2bba2ce90964e02a8383d4f29
MD5 1855fa4cc1f8504028311ccee435c871
BLAKE2b-256 6d3896d96b7f02ae0efb6af9570b957db6bc29998fd81ec8914ba659976eaef3

See more details on using hashes here.

Supported by

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