Skip to main content

As the name says, it's a little helper for logging more comfortably in python 3 (for now only python3 support) to file and console!

Project description

betterLogs4Python

As the name says, it's a little helper for logging more comfortably in python 3 (for now only python3 support) to file and console!

Installation

For an easy installation install betterLogs4Python via pip:

pip3 install betterLogs4Python

For an executable Demo or test run Demo.py.

How To Use

  1. Create a BetterLogger object (maybe with parameters) All Parameters are optional:

    Parameter Default Description
    logFilename "app.log" (String) Defines the filename/path the BetterLogger will log to if printToFile is True.
    loggerName "" (String) Defines a module name that will be added to the log message.
    includeTime False (Bool) Defines if the log time should be included.
    printToConsole True (Bool) Defines if the log messages should be printed to stdout (Console output).
    printToFile True (Bool) Defines if the log messages should be printed to file (specified in logFilename parameter).
    lineLimiter -1 (Int > 0) Defines the max number of lines in the log file. (-1 means no limit) (When exceeding the limit, old logs get deleted).
    runLimiter -1 (Int > 0) Defines the max number of runs in the log file. Runs are separated with String defined in runSeparator parameter. (-1 means no limit) (When exceeding the limit, old runs get deleted).
    logLevelThreshold 0 (Int) Defines the threshold of log level number that get logged (Message will only get printed to File or Console if it's logLevelNum > logLevelThreshold. Else the log will be ignored. Default logLevelNum is 20).
    runSeparator "----------" (String) Defines the String that defines the end of a run. It gets used to retain the runLimit and gets logged when finishRun function is called. (If you normal log contains the runSeparator String it counts as the runSeparator)
    logger = BetterLogger()
    
  2. Log to file by calling the log function with the logLevelNum parameter to define the type of the log message (default ist 20). The logLevelNum corresponds to the types as follows:

    Number Type
    10 "DEBUG"
    20 (Default) "INFO"
    30 "WARNING"
    40 "ERROR"
    50 "FATAL"
    logger.log("Hello this is an Debug!",logLevelNum=10)
    

    You can also add new log types:

    logger.addLogLevelName("EXECUTION",25)
    logger.log("Hello this the new execution type!",logLevelNum=25)
    
  3. If using the runLimiter maybe call the finishRun function to finish the run.

    logger.finishRun()
    
  4. Enjoy the Output :)

Simple logger

# After getting a logger object with the logfile path,
# you can log by calling the log function with optionally a logLevel that defines what type of log Message it is:
#       10 : "DEBUG",
#       20 : "INFO",
#       30 : "WARNING",
#       40 : "ERROR",
#       50 : "FATAL"

# Defaults of BetterLogger class: logFilename="app.log", loggerName="",includeTime=False,printToConsole=True,printToFile=True,lineLimiter=-1, runLimiter=-1, logLevelThreshold=0, runSeparator="----------"
# For a simple Log you can use the defaults!

logger = BetterLogger(logFilename="test.log")

logger.log("Hello this is an Info!") # Default log Level is 20 (INFO)
logger.log("Hello this is an Debug!",logLevelNum=10)
logger.log("Hello this is an Error!",logLevelNum=40)

# You can add new log message types with a new logLevelNumber
logger.addLogLevelName("EXECUTION",25)
logger.log("Hello this the new execution type!",logLevelNum=25)


# Output will be to the test.log file and to console:
#[INFO] Hello this is an Info!
#[DEBUG] Hello this is an Debug!
#[ERROR] Hello this is an Error!
#[EXECUTION] Hello this the new execution type!

More advanced logger

# Example for a Logger that logs only to the File "test.log". (Not the Console!)
# The log includes Timestamps and a module name "testModule". 
# The number of Lines in the Logfile is limited to 15 lines (oldest lines get removed when exceded!).
# The Logger will only write logs if the given logLevel is greater than 10 (10 is not included)  
logger = BetterLogger(logFilename="test.log",includeTime=True,loggerName="testModule",printToConsole=False,printToFile=True,lineLimiter=15,logLevelThreshold=5)

logger.addLogLevelName("Test",5)
logger.log("Hello this is a less important Test type!",logLevelNum=5) # Won't get printed because the logLevel is not greater than the logLevelThreshold (5)

logger.addLogLevelName("Important",100)
logger.log("This is a more Important message!",100)
# The Output will only be in the file "test.log"
#[2021-09-05 07:00:40,239] testModule: [Important] This is a more Important message!

Run limited logger

# Example for a Logger that logs to the File "test.log" and the Console.
# The Log contains no Timestamp and no module name. 
# The log file only stores 2 runs seperated by "-----" (older runs get deleted)
logger = BetterLogger(logFilename="test.log",runLimiter=2,runSeparator="-----")
logger.log("Test")
for i in range(4): # Simulate 4 runs
    logger.log("Info-Log of 1 run!")
    logger.log("Oh Error in run: "+str(i+1),40)
    logger.finishRun()

# Output in console includes logs for all 4 runs but in file are only logs for run 3 and 4 (last 2 runs):
#[INFO] Info-Log of 1 run!
#[ERROR] Oh Error in run: 3
#-----
#[INFO] Info-Log of 1 run!
#[ERROR] Oh Error in run: 4
#-----

When you notice any errors, have a problem using this software or have a suggestion for improvement, feel free to open an issue or a pull request

License

This software is licensed under the MIT License:

MIT License

Copyright (c) 2021 Jonas Mayer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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

betterLogs4Python-0.0.6.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

betterLogs4Python-0.0.6-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file betterLogs4Python-0.0.6.tar.gz.

File metadata

  • Download URL: betterLogs4Python-0.0.6.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for betterLogs4Python-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e1ff4db5e677184768abc67c7c8820b4dfb4868f78eb2943e403185db38aaa21
MD5 9be5b5bc74e0676fb5226b47cee56287
BLAKE2b-256 4741c54ddb7b37141bde581977bed5a261ab135eaed08d5861788e1e24b8eb28

See more details on using hashes here.

File details

Details for the file betterLogs4Python-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: betterLogs4Python-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for betterLogs4Python-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e1c3b31e5e3cb567220e48977473ca1bb7a954931c20a5c3e66ede4fe197486a
MD5 b6b22471ecd7fe7f16a7ec043ae18673
BLAKE2b-256 54d3d0786478f18bb4260518b730c78dd095d4f3d326f746cde21dc7e337a302

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