This module offers universal logging solution for software development, taking into considerations things such as logfile title, managing existing logfiles, changing working directories, 4 log types, adjustable timestamp and log with pre- and postfixes.
It is also supplied with multithreading opportunity, using Events and Exception handling.
To use the LogPy, clone the repository into your directory using Git:
$ git clone https://github.com/PiotrJZielinski/LogPyYou can then (assuming you put LogPy module in your working directory) import it into your project using:
from LogPy.LogPy import LoggerYou might as well install the package using pip:
pip install git+https://github.com/PiotrJZielinski/LogPy
The class is initialized with parameters:
Logger(filename='main', directory='', logtype='info', timestamp='%Y-%m-%d | %H:%M:%S.%f', logformat='[{timestamp}] {logtype}: {message}', prefix='', postfix='', title='Main Logger', logexists='append', console=False):where:
filenameis the name of the file in which you intend to put the log in;directoryis the directory in which you want to put the file; leaving it default puts the file in your working directory;logtypeis the default log message to use (one of: info, warning, error, fatal);timestampis the string used for defining (see datetime.strftime() for timestamp formatting details);logformatis the log configuration (string containing variables: timestamp, logtype, message, prefix, postfix)prefixpostfixtitleis the string to be put on the top of the logfilelogexistsis the default action to be performed in case logfile already exists (append, overwrite or rename)consoleis a boolean specifying whether the logger should print messages in the consoleexternal_functionis a reference to function that returns string message for loginternal_logger_timedelay between external function calls
You can change most of these parameters during the operation of the program using property setters supplied in the module
Methods such as clear, delete, pause and resume are to perform as they are named. They are provided with security checks and necessary features.
For logging use the method
log(msg, logtype=''):where:
msgis the message to be put in the logfilelogtypeis the log details put before the message (one of the available) - defaults to the type defined in the initialization
Logging with a message starting with an exclamation mark ('!') will disable all information, putting just the message in the row.
If you assign a reference to class to external_function, internal thread calls it at regullar intervals. Size of the intervals equals internal_logger_time value
The class is designed for running as a separate thread, which would provide exception catching for the log without interrupting main program execution and waiting for other instructions to execute. In order to take advantage of the threading use similar code:
from threading import Thread
from LogPy.LogPy import Logger
logger = Logger()
logger_thread = Thread(target=logger.run, name='My Logger')
logger_thread.start()Alternative method for starting logger thread - call start method:
logger = Logger()
logger.start()From then on the thread will be running. To log something use the aforemention log method.
In order to stop the logger from running (ie. at the end of your program) you have to set an exit flag. To do so simply type:
logger.exit()which will interrupt thread execution, terminating it.