Giter Club home page Giter Club logo

logpy's Introduction

LogPy Logger - the versatile Python Logger

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.

Usage

To use the LogPy, clone the repository into your directory using Git:

$ git clone https://github.com/PiotrJZielinski/LogPy

You can then (assuming you put LogPy module in your working directory) import it into your project using:

from LogPy.LogPy import Logger

You might as well install the package using pip:

pip install git+https://github.com/PiotrJZielinski/LogPy

Initialization

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:

  • filename is the name of the file in which you intend to put the log in;
  • directory is the directory in which you want to put the file; leaving it default puts the file in your working directory;
  • logtype is the default log message to use (one of: info, warning, error, fatal);
  • timestamp is the string used for defining (see datetime.strftime() for timestamp formatting details);
  • logformat is the log configuration (string containing variables: timestamp, logtype, message, prefix, postfix)
  • prefix
  • postfix
  • title is the string to be put on the top of the logfile
  • logexists is the default action to be performed in case logfile already exists (append, overwrite or rename)
  • console is a boolean specifying whether the logger should print messages in the console
  • external_function is a reference to function that returns string message for log
  • internal_logger_time delay 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.

Logging

For logging use the method

log(msg, logtype=''):

where:

  • msg is the message to be put in the logfile
  • logtype is 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.

Periodic logging

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

Running

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.

logpy's People

Contributors

piotlinski avatar dominikwasiolka avatar

Forkers

dominikwasiolka

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.