A Logging Library for Python
Project description
Logsmith
Logsmith is a Logging Library for python apps with support for logsmith-monitor.
Getting Started
Install Logsmith.py using pip
pip install logsmith
Import the Logsmith
object to use Logsmith.py
from logsmith import Logsmith
log = Logsmith({})
log.INFO("this is working fine")
To ensure backward compatibility, the previously usable class 'log', that was the main accss point to the library, inherits from the Logsmith class and can be used interchangeably with 'Logsmith' class. Example:
from logsmith import log
log = log({})
log.INFO("this is working fine")
But, the log class will be phased out in favour of the Logsmith class and hence we recommend using the Logsmith class in your code for future proofing.
Configurations
While a Logsmith.py supports a no-config setup, there are some configurations that can be tweaked as per need. Read More
For Logsmith-Monitor Support, go here.
Usage
Logsmith.py provides several Log Level based logging methods. It also provides a customizable logging method that can be used to log with a custom log level other than the ones provided. The methods can take both JSON and Statements as valid log.
log.SUCCESS()
log.SUCCESS()
can be used to log Successful events and actions. Example:
log.SUCCESS({"test" : "passed"})
log.SUCCESS("The Tests Passed!")
log.INFO()
log.INFO()
can be used to log Informational messages/prompts. Example:
log.INFO({"status" : "ok"})
log.INFO("The System is OK")
log.WARN()
log.WARN()
can be used to log Warnings. Example:
log.WARN({"status" : "The system may be Vulnerable"})
log.WARN("The system may be Vulnerable")
log.FAILURE()
log.FAILURE()
can be used to log Failures. Example:
log.FAILURE({"test" : "failed"})
log.FAILURE("The Tests Failed.")
log.CRITICAL()
log.CRITICAL()
can be used to log events that may lead to system shutdowns, data loss or other critically fatal events. Example:
log.CRITICAL({"system" : "meltdown"})
log.CRITICAL("The system is Shutting Down.")
log.LOG()
This method provides the custom log level logging capability. This method takes a parameter loglevel
other than the log itself. The loglevel can be set to any string. Example:
log.LOG(loglevel="TEST", {"test": "passed"})
log.LOG(loglevel="TEST", "The test Passed")
Configurations
To use logsmith, certain configurations are required. If no configs are provided then the default values are set. A basic logsmith config looks something like this.
{
"env": "test",
"logfile": "path/to/local/log/file",
"consoleOnly": false,
"logStatementPattern": "[ {component} ~ {logLevel} ] : {message}",
"logFormat": "json",
"monitorLogging": true
}
The Various flags/fields that can be configured are
Field | Description | Type | Allowed Values | Default Value |
---|---|---|---|---|
env | Environment on which the app is running | string | Any | "default" |
consoleOnly | If consoleOnly is set to true then the logs will only be shown on the terminal, and wont be logged to any file | boolean | true or false |
true |
logfile | If logs are to be logged to a file, this field is used to specify the path to the logfile, works only if consoleOnly is false | string | Relative Path to File | null |
logStatementPattern | The Pattern in which the log needs to be logged on console. Read More. | string | Any String | [{timestamp}] {message} |
logFormat | The Format of Log | string | json or statement |
json |
monitorLogging | Flag that is set if logging to monitor is required. If set to true, logs will be published to monitor too. If set to true, monitor specific configurations are also required. Read More | boolean | true or false |
false |
monitor | A JSON field that is required to communicate with monitor. Works only is monitorLogging is set to true. Read the Monitor Config Section for more | json | Read More | null |
Using Configurations
Configurations can be defined and used in an application by creating the config json and passing it to the Logsmith object while initializing.
from logsmith import Logsmith
logConfig = {
"env": "test",
"logfile": "path/to/local/log/file",
"consoleOnly": False,
"logStatementPattern": "[ {component} ~ {logLevel} ] : {message}",
"logFormat": "json",
"monitorLogging": True
}
log = Logsmith(logConfig)
Configurations may also be loaded from JSON files. Define the Configurations in a JSON file and load it using fetchConfigFromFile()
method.
from logsmith import Logsmith
log = Logsmith({})
log.fetchConfigFromFile(filepath="/path/to/config/file.json")
Support for Logsmith Monitor
Logsmith Monitor (or simply Monitor) is a stand-alone logging Server for multi-component apps. Read about logsmith-monitor here.
Logsmith.py supports logging to Monitor. It supports creation of Publishers and Context namespaces.
Monitor Configurations
There are some specific connfigurations that are required to log to logsmith monitor. A basic and minimum configuration has the following fields, along with the configurations mentioned above.
The Configurations for monitor support are defined as a JSON Object with "monitor" as the key.
{
"monitor": {
"port": "8080",
"server": "localhost",
"publisher": {
"publisher": "testapp001"
},
"context": {
"context": "testcontext001"
}
}
}
The fields required for monitor support are
Fields | Description | Type | Allowed Values |
---|---|---|---|
server | The address/URI where monitor is running | URI (string) | URL |
port | The Port of the address where its running | string | Numeric and Valid PORT number |
publisher | A JSON object to define the details of the Publisher | JSON Object | JSON |
context | A JSON Object top specify the details of the Context | JSON Object | JSON |
The publisher
and context
fields are used to define their respective configs. The bare minimum publisher and context information that must be provided are the namespaces.
For Publisher Configurations, the field that needs to be put is the publisher name. Eg:
"publisher": {
"publisher": "<publsiher-name>"
}
For Context Configurations, the field that needs to be put is the context name. Eg:
"context": {
"context": "<context-name>"
}
There are other customizable fields for either. These fields are same as that defined for running Monitor.
When these values are not mentioned/provided by the user, logsmith creates/generates those for you. So the Configuration Above becomes
"monitorConfigs": {
"monitorPort": "8080",
"monitorURI": "localhost",
"monitorProtocol": "http",
"monitorListener": "http://localhost:8080",
"publisher": {
"publisher": "testapp001",
"origin": "app.testapp001.com",
"description": "Logs Published by testapp001"
},
"context": {
"context": "testcontext001",
"origin": "app.testapp001.com/testcontext001",
"description": "Logs Published by testapp001",
"kind": []
}
}
Initializing Monitor Connection
Logsmith provides a method - prepareMonitor()
, to initialize a connection with the monitor. The method checks if the Publisher and Context Namespaces are available in monitor and creates them if not.
from logsmith import Logsmith
log = Logsmith({})
log.fetchConfigFromFile(filepath="/path/to/file.json")
// initialize Monitor
log.prepareMonitor()
Know More
Some general information about some of the components in Logsmith.py.
Log Statement Patterns
Log Statement Patterns are strings with placeholder that are used to print logs on-to the local console in the same format. The placeholders are identified by the names of the fields (in log) and placeholders names are defined in the string by enclosing them within curly-braces {}
.
Example Usage
from logsmith import Logsmith
logStatementPattern = "{timestamp} > {status}"
log = Logsmith({"logStatementPattern" : logStatementPattern})
log.INFO({"timetamp": "11 AM", "status" : "ok", "action": "create"})
The above code logs a custom log statement
11 AM > ok
Example Application
To test Logsmith and Logsmith-Monitor in Action, we created this example application.
Startup Logsmith Monitor in Docker
make run-monitor
make show-monitor
Start the example Python App (Script).
python3 example.py
The logs should be printed both on the monitor as well as the example app terminal. Play around with the config for more customization.
Teardown the Monitor Container after testing.
make teardown-monitor
Known Issues
There are some issues that we are working on to solve. List of the known issues and their temporary remediation are available here.
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
Built Distribution
File details
Details for the file logsmith-0.1.0.tar.gz
.
File metadata
- Download URL: logsmith-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd3c7836c1d908ebcdffa441f542977ce7b49a0620b89b823c3ffe52c83af5bd |
|
MD5 | 32dfd345b8cf8489334002f00b2db9a9 |
|
BLAKE2b-256 | fda0f315729a3c47d819d80fad643b1e967369c7643f57c314f5d322b579919b |
File details
Details for the file logsmith-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: logsmith-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3898b8de56fa573b2886a4d100e8521a19e634dcb419592211830fb5bd73d32 |
|
MD5 | 5cc4187c56fdab59dcd6db325786dbe4 |
|
BLAKE2b-256 | 36707988d83ef00371906c6d4ed4d7385f2bd58ab9167e23f3067a505e27f751 |