Skip to main content

# SingleLog

Project description

SingleLog

Package Version test PyPI - Downloads PyPI - Python Version

SingleLog is a simple library for logging. It is designed to make logging easier.

It can format your data and output it to the console or file.

It can also log your event status step by step.

Quick View

Stage Method

You can use stage method to display the log step by step in the same line.
And don't need to worry about the default print function will print the log in the same line.

Note that the stage effect only works on LogLevel.INFO.

import time
from SingleLog import Logger

logger = Logger('rocket')

logger.info('Init rocket launch proces')
time.sleep(1.5)
logger.stage('complete!')

logger.info('Start the countdown')
time.sleep(1)
logger.stage('3')
time.sleep(1)
logger.stage('2')
time.sleep(1)
logger.stage('1')
time.sleep(1)
logger.stage('fire!')
logger.info('Launch complete')

Automatic Data Format

SingleLog Supports some common types to display in format. Such as list, dict and tuple etc.

from SingleLog import Logger

logger = Logger('demo')
logger.info('show int list', [101, 102, 103])
logger.info('show tuple', ('12', '14', '16'))
logger.info('data', {'1': 'value1', '2': 'value2'})

Install

pip install SingleLog -U

Basic Usage

from SingleLog import Logger

logger = Logger('demo')

logger.info('hello world')

Log Levels

You can set the LogLevel of the logger.

  • info: Confirmation that things are working as expected.
  • debug: Detailed information, usually of interest only when diagnosing problems.
  • trace: Detailed information on the flow through the system.
from SingleLog import Logger
from SingleLog import LogLevel

logger = Logger('demo', log_level=LogLevel.DEBUG)

logger.info('you can see it, when log_level is set to INFO, DEBUG and TRACE')
logger.debug('you can see it, when log_level is set to DEBUG and TRACE')
logger.trace('you can see it, when log_level is set to TRACE')

Logger Handler

You can use logger handler to handle the log.

from SingleLog import Logger

def log_to_file(msg):
    with open('single_log.txt', 'a', encoding='utf8') as f:
        f.write(f'{msg}\n')


logger = Logger('Handle', handler=log_to_file)

logger.info('1')
logger.info(2)
logger.info('show value', 456)

Handler also supports list.

from SingleLog import Logger
from SingleLog import LogLevel

def log_to_file(msg):
    with open('single_log.txt', 'a', encoding='utf8') as f:
        f.write(f'{msg}\n')

def log_to_file2(msg):
    with open('single_log_2.txt', 'a', encoding='utf8') as f:
        f.write(f'{msg}\n')


logger = Logger('INFO', LogLevel.INFO, handler=[log_to_file, log_to_file2])

logger.info('1')
logger.info(2)
logger.info('show value', 456)

In this demo, the log message will display on the screen.
Also you can find it in single_log.txt and single_log_2.txt.

Advanced Usage

You can use logger to display the log in different ways.

Skip Repeated Message

You can skip repeated message.
The following logger will display only one message.

from SingleLog import Logger

logger = Logger('demo', skip_repeat=True)

logger.info('hello world')
logger.info('hello world')
logger.info('hello world')

Stage Separator

You can change stage separator to display the log.

import time
from SingleLog import Logger

logger = Logger('demo', stage_sep='-')

logger.info('Init rocket launch proces')
time.sleep(1.5)
logger.stage('complete!')

logger.info('Start the countdown')
time.sleep(1)
logger.stage('3')
time.sleep(1)
logger.stage('2')
time.sleep(1)
logger.stage('1')

Stage Color

You can change stage color.
You can find the more color information from Colorama.

from colorama import Fore
from SingleLog import Logger

logger = Logger('demo', stage_color_list = [Fore.GREEN, Fore.YELLOW])

logger.info('start')
for i in range(10):
    logger.stage(i)

Timestamp

You can change timestamp format.

from SingleLog import Logger

logger = Logger('demo')
logger.info('default timestamp')

logger = Logger('demo', timestamp='%Y-%m-%d %H:%M:%S')
logger.info('custom timestamp')

Keyword of success and failure

You can use keyword of success and failure to display the log.

The keywords in key_word_success, will be displayed in green color.
The keywords in key_word_failure, will be displayed in red color.

from SingleLog import Logger

logger = Logger('demo', key_word_success=['custom_success'], key_word_fails=['custom_fail'])

logger.info('do something')
logger.stage('custom_success')
logger.stage('custom_fail')

License

MIT License

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

SingleLog-2.0.14.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

SingleLog-2.0.14-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file SingleLog-2.0.14.tar.gz.

File metadata

  • Download URL: SingleLog-2.0.14.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for SingleLog-2.0.14.tar.gz
Algorithm Hash digest
SHA256 50d72824633a3235b93e38ed053c25e98d58a7bfe1b1dd5f2f7459b46da784c1
MD5 aad55af86bea076e9f841293a58f0bd0
BLAKE2b-256 889aa2d88865594b96163d808f4a7e3c867d1884380540e7737b733e82d26a65

See more details on using hashes here.

File details

Details for the file SingleLog-2.0.14-py3-none-any.whl.

File metadata

  • Download URL: SingleLog-2.0.14-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for SingleLog-2.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 dd3d2d0204e9ca196607d387351dee8301502ff813e23ca9b425bfb0bf22eca1
MD5 4950bb39f39a0f1d0ec29b95a5ede489
BLAKE2b-256 3081d79a54856e1caab4fe6c284f85392f7456153701cad71ea28a864e0a2096

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