Pretty Logs Demo
Download plogs for python3

Project Goals

In the beginning, the goal of Pretty Logs was to create a colorful logging system designed for scalable projects. Later, my vision for Pretty Logs widened in scope to also include useful debugging tools that are found in JavaScript and aren't that readily available in Python.

In the future, I anticipate better control to configure logging settings and a debugging log level where debug tools can only print to.

Install

The easiest way to install Pretty Logs is to install via pip.

$ pip3 install -U plogs

Is also possible to download and import Pretty Logs directly into your project. If you are you looking to do so, I recommend cloning the GitHub repository to ensure the plogs module hierarchy is kept the same.

$ git clone https://github.com/11/plogs.git

Setup

Importing Pretty Logs into your project is quite simple. All that's required is to import plogs and instantiate an instance of Logger.

from plogs import get_logger
logging = get_logger()

It's recommend to do the following steps inside an `__init__.py` file at the root level of your project so Pretty Logs can be referenced throughout the entire application.

Log with Colors

Pretty Logs' main feature is color coding different logging levels and statues. The default logging levels are:


logging.info gray
logging.status bold
logging.success green
logging.warning orange
logging.error red
logging.critical red highlight

Log to a File

Pretty Logs can write colored logs to files. This is done through Pretty Logs' config function.


from plogs import get_logger

logging = get_logger()
logging.config(to_file=True)

logging.info('this is will be written to a file')
                

By default, files are written to `/var/log/plogs/plog_01.log`. Thee reason that `/var/log/` is chosen as the default directory is because of how often it's included in unix machines and docker images.

If you are looking to use another filename and location, it can simply be edited like such:


logging.config(
    to_file=True,
    file_location='your/filepath/here/',
    filename='new_file.log'
)
                

Note: It's recommended to view colored logs with the less terminal command - if less doesn't work be default, trying opening a log file with `less -r`. Also, Pretty Logs was not designed to show colored files in Vim, Atom, Emacs, Sublime, and other popular text editors.

Formatting Logs

Pretty Logs allows for a lot of customization. Customizing logs is done by editing the logging config and supplying Pretty Logs with a formatted string.

The following are all the configurable variables:

pretty bool N/A Setting to True will add color to logs, False will un-color logs
show_levels bool {level} Setting to True will show logging level in formatted log, False show no logging level
show_time bool {time} Setting to True will show time in formatted log, False doesn't show time
to_file bool N/A Setting to True writes logs to file_location, False writes to standard output
file_location str {file_location} Default file_location is `/var/log/plogs/`, otherwise a file location of your choice
filename str {filename} Default log file is `plog_01.log`, otherwise a filename of your choice
N/A N/A {msg} Shows the log message in formatted log

An example of a formatted log would be like such:


from plogs import get_logger

logging = get_logger()

# configure plogs to allow logging level and date/time
logging.config(show_levels=True, show_time=True)

# config logs with the `{level}` keyword to show the logging level,
# `{time}` to show the date & time of when the log was written
# and `{msg}` to show the logging message
logging.format('[{level}] - {time} - {msg}')

# finally write logs
logging.status('Show me the logs')
logging.info('We got some info')

# Output:
# [STATUS] - 2018-12-11 11:56:05 - Show me the logs
# [INFO] - 2018-12-11 11:56:09 - We got some info

NOTE: In order to format Pretty Logs, the variable you want written must be inside a pair of `{}` - ie like so: `{filename}`, `{file_location}`, `{levels}`, etc. These values will be substituted for the variable they represent

Log Tables

Pretty Logs can print objects in a table and display the variables that each is holding.


ex1 = Example(a=1, b=2)
ex2 = Example(a='a', b='b')

logging.table(ex1, ex2)
                

The output of these two objects would be:

+ --------------- +
| | a | b |
+ --- | --- | --- +
| ex1 | 1 | 2 |
+ --- | --- | --- |
| ex2 | 'a' | 'b' |
+ --------------- +