Skip to main content

A simple json file logger for Python

Project description

📖 A Python json-filelogger

Lint code and run tests pypi

A simple way to log data to json-files from within your code.

Installation

You can use the python package manager (pip) to install the file-logger:

pip install jsonfilelogger

Usage

Create a LogWriter:

from jsonfilelogger.logger import LogWriter

writer = LogWriter(folder="./", filename="log.json", threshold=10)

Log data to the logfile:

writer.log({"key_int": 1,
            "key_string": "logger",
            "key_boolean": True,
            "key_double": 1.337,
            "key_list": ["element1", "element2"],
            "key_none": None})

Create a LogReader:

from jsonfilelogger.logger import LogReader

reader = LogReader(folder="./", filename="log.json")

Read data from the logfile:

data = reader.retrieve()

Other methods:

Class Method Explanation
LogReader, LogWriter .reset() This method clears the contents of the logfile.
LogReader, LogWriter .remove() This methode removes the logfile.
LogWriter .with_datetime(folder: str, threshold: int) This method creates a logfile in the folder with the current timestamp as filename.
LogWriter .log(data) This method logs the data (dictionary) in the logfile.
LogWriter .flush() This method flushes the buffer in the writer. The threshold of the buffer is given in the constructor
LogReader .retrieve() This method retrieves all the data from the logfile and returns it as a list of dictionaries.

Example usage

An example of how to use this logger is given. Imagine one has created a major breakthrough AI system that still has to be trained. During training one want's to keep an eye on the performance of the progress. To do this, the LogWriter can be added in the learning iterations (e.g. in one Jupyter notebook). In another process (e.g. in another Jupyter notebook) it is then possible to read in the data and make amazing visualisations (yay, visualisations!) of how your breakthrough model is performing...

from jsonfilelogger.logger import LogWriter

writer = LogWriter.with_datetime(folder="./", threshold=10)

super_great_ai_model = ...
iteration = 0
while iteration < 100_000:
    score, other_data = super_great_ai_model.do_iteration()
    writer.log({
        score: score,
        other_data: other_data
    })
    iteration += 1
writer.flush()
from jsonfilelogger.logger import LogReader
import glob

logs_files = sorted(glob.glob("./[0-9]*.json"))
reader = LogReader(folder="./", filename=logs_files[-1])

data = reader.retrieve()

# Make awesome visualisations!
...

There may also be other use cases, you can use this logger as you want!

Tests

You can run the tests yourselves by:

cd tests
pip3 install -r requirements.txt
pytest

License

This software is licensed under the MIT license.

Bugs/ Ideas?

In case of questions, ideas or bugs you can always create an issue or contact me.

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

jsonfilelogger-1.0.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonfilelogger-1.0.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file jsonfilelogger-1.0.3.tar.gz.

File metadata

  • Download URL: jsonfilelogger-1.0.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for jsonfilelogger-1.0.3.tar.gz
Algorithm Hash digest
SHA256 cbf6f773535e56be9e9db8dca1cd49c2b88f257a45cfbeae6aaf70ab6ed4397b
MD5 d66a43f0a2bb3bc42edec3816b5ccece
BLAKE2b-256 77ac4cf56ab5c1a716486064fd0f84742763d4caf5d8b9472b0441088b7e2738

See more details on using hashes here.

File details

Details for the file jsonfilelogger-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: jsonfilelogger-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for jsonfilelogger-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dedf1778d3651fc6128ca097d427694aecaeb187bf983203edf6aa33e5f08fa4
MD5 017946b5eeb35aa5a6ac23a954a90c09
BLAKE2b-256 f0b278c581058c983c05644a244fad776393251b628f8db1b03f97d1e7bfc02b

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