Skip to main content

A custom logging tool that expands normal logger with additional formatting and debug capabilities.

Project description

Shouter Usage Examples

The Shouter class is designed for managing and displaying formatted log messages, utilizing Python's logging module.

import sys
sys.path.append('../')
from python_modules.shouterlog import Shouter
# optional
import logging

Usage examples

The examples contain:

  1. initialize Shouter class
  2. basic usage like logging
  3. using different output types
  4. custom logger configuration
  5. backwards compatibility with existing loggers
  6. built-in records from Shouter usage
  7. debugging capabilities of Shouter

1. Initialize Shouter Class

shouter = Shouter(
    # optional/ required
    supported_classes = (),
    # optionally 
    ## Formatting settings
    dotline_length = 50,
    auto_output_type_selection = True,
    # For saving records
    tears_persist_path = '../env_spec/log_records.json',
    datetime_format = "%Y-%m-%d %H:%M:%S",
    # For saving env
    persist_env = False,
    env_persist_path = '../env_spec/environment.dill',
    ## Logger settings
    logger = None,
    logger_name = 'Shouter',
    loggerLvl = logging.DEBUG,
    logger_format = '(%(asctime)s) : %(name)s : [%(levelname)s] : %(message)s'
)

2. Basic usage like logging

shouter.debug(
    # optional
    dotline_length=30)
shouter.debug("This is a debug message!")
shouter.info("This is an info message!")
shouter.warning("This is a warning message!")
shouter.error("This is an error message!")
shouter.fatal("This is a fatal message!")
shouter.critical("This is a critical message!")
(2024-01-14 22:39:52,280) : Shouter : [DEBUG] : ==============================
(2024-01-14 22:39:52,292) : Shouter : [DEBUG] : This is a debug message!
(2024-01-14 22:39:52,296) : Shouter : [INFO] : This is an info message!
(2024-01-14 22:39:52,304) : Shouter : [WARNING] : This is a warning message!
(2024-01-14 22:39:52,307) : Shouter : [ERROR] : This is an error message!
(2024-01-14 22:39:52,310) : Shouter : [CRITICAL] : This is a fatal message!
(2024-01-14 22:39:52,313) : Shouter : [CRITICAL] : This is a critical message!

3. Using different output types

# Different types of outputs
shouter.info(output_type="dline")
shouter.info(output_type="HEAD1", mess="Header Message")
(2024-01-14 22:39:52,399) : Shouter : [INFO] : ==================================================
(2024-01-14 22:39:52,402) : Shouter : [INFO] : 
==================================================
-----------------Header Message----------------- 
==================================================

4. Custom logger configuration

import logging

# Custom logger
custom_logger = logging.getLogger("CustomLogger")
custom_logger.setLevel(logging.INFO)

# Shouter with custom logger
shouter_with_custom_logger = Shouter(supported_classes=(), logger=custom_logger)
shouter_with_custom_logger.info(mess="Message with custom logger")
(2024-01-14 22:39:52,429) : CustomLogger : [INFO] : Message with custom logger

5. Backwards compatibility with existing loggers

import logging
import attr #>=22.2.0


@attr.s
class ExampleClass:

    # Logger settings
    logger = attr.ib(default=None)
    logger_name = attr.ib(default='Example Class')
    loggerLvl = attr.ib(default=logging.DEBUG)
    logger_format = attr.ib(default='(%(asctime)s) : %(name)s : [%(levelname)s] : %(message)s')

    def __attrs_post_init__(self):
        self.initialize_logger()

    def initialize_logger(self):

        """
        Initialize a logger for the class instance based on
        the specified logging level and logger name.
        """

        if self.logger is None:
            logging.basicConfig(level=self.loggerLvl,format=self.logger_format)
            logger = logging.getLogger(self.logger_name)
            logger.setLevel(self.loggerLvl)

            self.logger = logger
            
    def print_debug(self):
        
        self.logger.debug("This is a debug message!")
        
    def print_info(self):
        
        self.logger.info("This is a info message!")
        
    def print_warning(self):
        
        self.logger.warning("This is a warning message!")
        
    def print_error(self):
        
        self.logger.error("This is a error message!")
        
    def print_critical(self):
        
        self.logger.critical("This is a critical message!")
        
    def perform_action_chain_1(self):
        
        self.logger.debug("Action 1")
        self.print_debug()
                
        self.logger.debug("Action 2")
        self.print_error()
        
    def perform_action_chain_2(self):
                
        a = 1
        b = 'b'
        c = ['list']
        d = {'key' : 'value'}
        e = Shouter()
        
        self.logger.error("Saving env")
ec = ExampleClass()

ec.print_debug()
ec.print_info()
ec.print_warning()
ec.print_error()
ec.print_critical()
(2024-01-14 22:39:52,478) : Example Class : [DEBUG] : This is a debug message!
(2024-01-14 22:39:52,482) : Example Class : [INFO] : This is a info message!
(2024-01-14 22:39:52,484) : Example Class : [WARNING] : This is a warning message!
(2024-01-14 22:39:52,486) : Example Class : [ERROR] : This is a error message!
(2024-01-14 22:39:52,488) : Example Class : [CRITICAL] : This is a critical message!
shouter_for_example_class = Shouter(
    supported_classes = (ExampleClass),
    tears_persist_path = '../env_spec/log_records.json'
    )

ec = ExampleClass(logger=shouter_for_example_class)

ec.print_debug()
ec.print_info()
ec.print_warning()
ec.print_error()
ec.print_critical()
ec.perform_action_chain_1()
(2024-01-14 22:39:52,506) : Shouter : [DEBUG] : This is a debug message!
(2024-01-14 22:39:52,508) : Shouter : [INFO] : This is a info message!
(2024-01-14 22:39:52,510) : Shouter : [WARNING] : This is a warning message!
(2024-01-14 22:39:52,512) : Shouter : [ERROR] : This is a error message!
(2024-01-14 22:39:52,518) : Shouter : [CRITICAL] : This is a critical message!
(2024-01-14 22:39:52,521) : Shouter : [DEBUG] : Action 1
(2024-01-14 22:39:52,522) : Shouter : [DEBUG] : + This is a debug message!
(2024-01-14 22:39:52,524) : Shouter : [DEBUG] : Action 2
(2024-01-14 22:39:52,526) : Shouter : [ERROR] : + This is a error message!

6. Built-in records from Shouter usage

shouter_for_example_class = Shouter(
    supported_classes = (ExampleClass),
    tears_persist_path = '../env_spec/log_records.json'
    )

ec = ExampleClass(logger=shouter_for_example_class)

ec.print_debug()
ec.perform_action_chain_1()
(2024-01-14 22:39:52,551) : Shouter : [DEBUG] : This is a debug message!
(2024-01-14 22:39:52,554) : Shouter : [DEBUG] : Action 1
(2024-01-14 22:39:52,556) : Shouter : [DEBUG] : + This is a debug message!
(2024-01-14 22:39:52,558) : Shouter : [DEBUG] : Action 2
(2024-01-14 22:39:52,560) : Shouter : [ERROR] : + This is a error message!
ec.logger.return_logged_tears()
[{'datetime': '2024-01-14 22:39:52',
  'level': 'debug',
  'function': 'ExampleClass.print_debug',
  'mess': 'This is a debug message!',
  'line': 33,
  'lines': [33],
  'traceback': ['ExampleClass.print_debug']},
 {'datetime': '2024-01-14 22:39:52',
  'level': 'debug',
  'function': 'ExampleClass.perform_action_chain_1',
  'mess': 'Action 1',
  'line': 53,
  'lines': [53],
  'traceback': ['ExampleClass.perform_action_chain_1']},
 {'datetime': '2024-01-14 22:39:52',
  'level': 'debug',
  'function': 'ExampleClass.perform_action_chain_1',
  'mess': 'This is a debug message!',
  'line': 54,
  'lines': [33, 54],
  'traceback': ['ExampleClass.print_debug',
   'ExampleClass.perform_action_chain_1']},
 {'datetime': '2024-01-14 22:39:52',
  'level': 'debug',
  'function': 'ExampleClass.perform_action_chain_1',
  'mess': 'Action 2',
  'line': 56,
  'lines': [56],
  'traceback': ['ExampleClass.perform_action_chain_1']},
 {'datetime': '2024-01-14 22:39:52',
  'level': 'error',
  'function': 'ExampleClass.perform_action_chain_1',
  'mess': 'This is a error message!',
  'line': 57,
  'lines': [45, 57],
  'traceback': ['ExampleClass.print_error',
   'ExampleClass.perform_action_chain_1']}]
import pandas as pd

pd.DataFrame(ec.logger.return_logged_tears())
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
datetime level function mess line lines traceback
0 2024-01-14 22:39:52 debug ExampleClass.print_debug This is a debug message! 33 [33] [ExampleClass.print_debug]
1 2024-01-14 22:39:52 debug ExampleClass.perform_action_chain_1 Action 1 53 [53] [ExampleClass.perform_action_chain_1]
2 2024-01-14 22:39:52 debug ExampleClass.perform_action_chain_1 This is a debug message! 54 [33, 54] [ExampleClass.print_debug, ExampleClass.perfor...
3 2024-01-14 22:39:52 debug ExampleClass.perform_action_chain_1 Action 2 56 [56] [ExampleClass.perform_action_chain_1]
4 2024-01-14 22:39:52 error ExampleClass.perform_action_chain_1 This is a error message! 57 [45, 57] [ExampleClass.print_error, ExampleClass.perfor...

7. Debugging capabilities of Shouter

shouter_for_example_class = Shouter(
    supported_classes = (ExampleClass),
    tears_persist_path = '../env_spec/log_records.json',
    persist_env = True,
    env_persist_path = '../env_spec/environment.dill'
)

ec = ExampleClass(logger=shouter_for_example_class)

ec.print_debug()
ec.perform_action_chain_2()
(2024-01-14 22:39:53,277) : Shouter : [DEBUG] : This is a debug message!
(2024-01-14 22:39:53,280) : Shouter : [ERROR] : Saving env
(2024-01-14 22:39:53,348) : Shouter : [WARNING] : Object 'self' could not have been serialized, when saving last words!
ec.logger.return_last_words(
    # optional
    env_persist_path = '../env_spec/environment.dill'
)
{'a': 1,
 'b': 'b',
 'c': ['list'],
 'd': {'key': 'value'},
 'e': Shouter(supported_classes=(), dotline_length=50, auto_output_type_selection=True, tears_persist_path='log_records.json', env_persist_path='environment.dill', datetime_format='%Y-%m-%d %H:%M:%S', log_records=[], persist_env=False, logger=<Logger Shouter (DEBUG)>, logger_name='Shouter', loggerLvl=10, logger_format='(%(asctime)s) : %(name)s : [%(levelname)s] : %(message)s')}

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

shouterlog-0.0.2.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

shouterlog-0.0.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file shouterlog-0.0.2.tar.gz.

File metadata

  • Download URL: shouterlog-0.0.2.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.15

File hashes

Hashes for shouterlog-0.0.2.tar.gz
Algorithm Hash digest
SHA256 986f6626b094c740c6168fffc4ffc6bf7dfcc790682fab370e2054f7ab39db2d
MD5 ff292fcf8f11c00d14b4ed0b2719a257
BLAKE2b-256 6fbf2f3c2daffdda6c9768b80a679407d23e6ca268a30b9430bebddbc95217c7

See more details on using hashes here.

File details

Details for the file shouterlog-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: shouterlog-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.15

File hashes

Hashes for shouterlog-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5a10dd77b7a2ab0e77dfe002f664a674de212f157ed38639e83a102b479d00c2
MD5 ad176d707a28f882b7895c5b7f7675e6
BLAKE2b-256 79973d46098684be11ba1523300e4c9d2b344a6d505bf9348dc2ece31fb1726f

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