Skip to main content

Helper for logging to files

Project description

fileloghelper

A simple helper for logging content to files (and displaying it)

Installation

pip3 install fileloghelper

class Logger

Used to log content to file and print output to the console

init(filename='log.txt', context='', verbose=True)

Logger will be configured to be saved to 'filename' if self.save() is called. 'context' is used to configure the behavior of logging/printing (see set_context()). If 'verbose', it will write extra information about the statement in the log file. If an error/warning is handled via show_error()/show_warning()/handle_exception(), it will write it's type in the same place.

verbose = False
>>> [context] [12:34:56] Hello World!
verbose = True
>>> [DEBUG] [context] [12:34:56] Hello World!
>>> [NotImplementedError] [context] [12:34:56] Hello World!

set_context(context)

Specifies context which will be added to all outputs (file & terminal) in front

parameter description
context: str string to be added to output of other functions

set_verbose(verbose)

Sets verbose mode for whole logger. If true, a info whether the text is success/debug/warning/error information will be added to the file

parameter description
verbose: bool value to set for verbose mode

save()

Saves the file under default/at declaration specified filename

clear()

Clear the log. Note: You have to save again to make changes to the actual file

success(text, display=True)

Writes log to file. If verbose mode active, '[SUCCESS]' will be written in addition.

parameter description
display: bool print text on console

debug(text, display=True)

Writes log to file. If verbose mode active, '[DEBUG]' will be written in addition.

parameter description
display: bool print text on console

warning(text, display=True, extra_context="")

Writes log to file. If verbose mode active, [{extra_context}] will be written in addition. extra_context can be used to give extra information about the warning.

parameter description
display: bool print text on console

error(text, display=True, extra_context="")

Writes log to file. If verbose mode active, [{extra_context}] will be written in addition. extra_context can be used to give extra information about the error.

parameter description
display: bool print text on console

plain(text, display=False, extra_long=False, very_plain=False)

Writes log without any colors to file. If display==True, the text will be displayed. If extra_long==True, milliseconds will be added to the timestamp. If very_plain==True, the timestamp will be omitted.

show_warning(warning: Warning, display=True)

Extracts class from warning and uses it to invoke warning() with extra_context

show_error(error: Exception, display=True)

Extracts class from error and uses it to invoke error() with extra_context

handle_exception(exception: Exception)

Automatically categorizes exception to invoke show_warning() or show_error()

header(sys_stat=False, date=False, description="", display=0)

Use plain() to output certain information:

parameter description
sys_stat write system information to the log
date write date information to the log
description write the specified description to the log
display also display certain information in the console (see table below)

Modes for display:

mode number information printed
0 (standard) none
1 description only
2 date only
3 system information only
4 description & date
5 description & system information
6 date & system information
7 all above

progress(x=0, description='', startx=0, maxx=100, mode='=', scale=10)

Easily create a progress bar. Startx is the minimal x value, maxx the maximum possible. 'mode' specifies the style ('=' / '#'). 'scale' indicates the length of the progress bar in characters. To update the progress bar, simply run the same method again and specify x to visualize the progress.

from fileloghelper import Logger

logger = Logger(filename='log.txt', context='MyContext')

logger.progress(description="Running 'Self-Destruction'")
Running 'Self-Destruction': 0.0% [>        ]

logger.progress(80)
Running 'Self-Destruction': 80.0% [=======> ]

Logger - Example

from fileloghelper import Logger

logger = Logger(filename='log.txt', context='MyLogger')

logger.header(sys_stat=True, date=True, description='A short description', display=7)

logger.debug('Hello World!', display=False)
logger.success('Successfull!', display=True)
logger.handle_exception(NotImplementedError("off to work!"))

logger.save()

class VariableObserver

Wrapper for variable with functions pre/post changing the variables's value and (for int and float) a history (a list, e.g. to plot with matplotlib)

init(value, pre_chance_func=lambda x: x, post_change_func=lambda x: x)

parameter description
value starting value of the variable
pre_change_func function called before the variable is changed
post_change_func function called after the variable is changed

Note: pre/post-change functions will be called with the value of the (main) variable. They will have to accept 1 positional argument.

set_value(new_value)

If new_value is different from the previous variable, it will trigger pre/post-change functions and change the value of the variable.

get_history()

If the type of the (main) variable is int or float, it has created a list of past values which can be get by calling this method

VariableObserver - Example

from fileloghelper import VariableObserver

def print_value(x):
    print("Current value:", x)

vo = VariableObserver(0, print_value, print_value)
for i in range(3):
    vo.set_value(i)
Current value: 0 [pre-change]
Current value: 1 [post-change]
Current value: 1 [pre]
Current value: 2 [post]
vo.get_history()
[0, 1, 2]

class VarSet

A set/collection of VariableObserver to make it easer to print larger streams of data to the console.

init(variables: dict[str, value])

print_variables()

Prints values of all variables in set to the console while overwriting old ones

VarSet - Example

from fileloghelper import VarSet
from time import sleep

myvars = {
    "x": 0,
    "y": 0
}
vs = VarSet(myvars)
for i in range(11):
    vs.variables.get("x").set_value(i)
    vs.variables.get("y").set_value(i * 2)
    vs.print_variables()
    sleep(0.5)

final output:

10, 20

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

fileloghelper-1.4.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

fileloghelper-1.4.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file fileloghelper-1.4.2.tar.gz.

File metadata

  • Download URL: fileloghelper-1.4.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.4

File hashes

Hashes for fileloghelper-1.4.2.tar.gz
Algorithm Hash digest
SHA256 93fdbaa333df3a199d31079e121edfb28febc97f6e1a3d9c239640901bbb9189
MD5 26a2902205cb51a93d442265fc7edf05
BLAKE2b-256 b8e4ab84caecb7ca2036fa39b2aed796170805517b9c4ceb0cfa683aa729c77a

See more details on using hashes here.

File details

Details for the file fileloghelper-1.4.2-py3-none-any.whl.

File metadata

  • Download URL: fileloghelper-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.4

File hashes

Hashes for fileloghelper-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 279942931a9bfe53d182af68d49cdba9d9ec55014759c8e09128440e1353c8a1
MD5 26ac2bdc6d9c93d083252daad8f0b2b2
BLAKE2b-256 d2be14bd4c4e9c2c4d9eb33c3c342355ef65e44a9bd1efb7c4d5a25f64eaba8a

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