Package: sl4p
SL4P : simplest logging for your pythonsl4pis a logging library that makes it easy to introduce more production-focused logging into your python applications.sl4p users need not know anything about the slightly complex built-in python logging, although sl4p uses built-in Python's logging internally.Quite simply,you can start logging right in any code with the three linesbelow.fromsl4pimport*log=sl4p.getLogger(__file__)log.info("Hello Sl4p logger !")Then, a log file with the contents below will be created in the folder./sl4p_logsin the same location as your code.INFO   | 2023.03.22-17:19:13 - embedding.py (  86) :: OPERATING TIMEZONE: +9:00
INFO   | 2023.03.22-17:19:13 - embedding.py (  87) :: Program  @c8abb11e  started.  <<ex1.py>>
DEBUG  | 2023.03.22-17:19:13 - embedding.py (  88) ::          @@-- argv[0] = C:\path\to\your\python\app.py
INFO   | 2023.03.22-17:19:13 - ex1.py   (   4) :: Hello Sl4p logger !
INFO   | 2023.03.22-17:19:13 - embedding.py ( 105) :: Program  c8abb11e  finished  <<ex1.py>>  -----  Elapsed    0.0010 sDon't waste your time learning logging. Just enjoy this!Main FeaturesHere are the main features that are very easy to use:BasicLogging that can be used right away with little to no setup required (by default config)Colored console log: Warning, Error, and Exception are output in progressively more emphasized colors (Python 3.6+)ConfigFile: Single/multi-application logger settings by JSON files and python dictionaryAuto purging: log files can be divided and recorded by specified time period or filesize, and old ones can be automatically deletedSnippet log: Logging to separate files for specific modules or subset of your Python program packageLogging in multi-threaded/multi-process(internally, without IPC, with recording to a same file)Log of program context info: Timezone info, program execution path, total program run time, etc.Utils & AdvancedSimpleTimer: a stopwatch logging utility that supports start and multiple checks@sl4p_time()records the time taken for function execution (optional user tag)@sl4p_try,@sl4p_try_exitautomatically log traceback using Try~Except statementsProfiling: record CPU and memory usage as a separate CSV file during program lifeExceptionHook: Register your function to execute in the event of an abnormal program terminationInstallationThe'sl4p'package can be easily installed using pip.$ pip install sl4pOr download the appropriate .whl file for your Python version from the 'dist' folder on GitHub.$ pip install sl4p-{pkg_ver}-{py_ver}-none-any.whlConfiguration (Single App)Sl4p has key concepts of'configuration'and'logging'.Configuration decides sl4p logger's behavior including logging level, message format, logfile savedir and so on.You can use Python dictionary or .cfg Yaml file to configuring as you want.The following table describes the available keys and their corresponding values (default values indicated with an asterisk):# Configured example{"__configver__":"C4",# For .cfg Yaml file, you must specify __configver__"LOG":{"use_console_print":true,"console_level":"WARNING","logging_level":"DEBUG","logging_format":"basic","logfile_savedir":"logs","logfile_name":"EXAMPLE_1",}}KeyDescriptionAvailable value (*default)use_console_printWhether to print log messages to the console.Bool(*) True, Falseconsole_levelThe minimum level of logs to be printed in console.Logs below this level will not be printed. If not be assigned, follow the logging_level.(*) "DEBUG", "INFO", "WARNING",  "ERROR", "CRITICAL"console_formatThe level of detail to include in the log message in console.  If not be assigned, follow the logging_format."simple", (*) "basic", "detail", "adap"console_stdoutWhether to send console output to stdout, otherwise to stderr which is the default of logging. (Output in red text in some IDEs)BoolTrue, (*) Falselogging_levelThe minimum level of logs to be recorded.Logs below this level will not be recorded.(*) "DEBUG", "INFO", "WARNING",  "ERROR", "CRITICAL"logging_formatThe level of detail to include in the log message in file."simple", (*) "basic", "detail", "adap"logfile_savedirThe absolute path to the directory where log files will be saved.The path should be in either Windows or Linux format.String: The OS-specific folder pathwindows ex) "E:\\Workspace\\sl4p_logs\\app-abc"linux ex) "/opt/my_appvar/data/sl4p_logs/app_abc"logfile_nameThe log file name for your application.(default: based on your python program's name)Sl4p will use this prefix to identify and purge old log files.String: OS-supported filename.ex) "LOG.your_app_name"save_period_typeThe time or size can be set as the criterion for dividing and saving log files.String: "B", "KB", (*) "MB", "GB" by filesize"S", "M", "H", "D" by timesave_period_intervalSpecifies the magnitude to use for the selected save_period_typeInt(*) 100save_files_nlimitWhether to split and save logs up to a maximum number of files. (Delete excess ones from oldest to newest)Int(*) 20purge_window_hoursA window hours that deletes old log files (as much as the hours before the current time)Int(*) 8800console_colorlog_kwargsThe dictionary that is passed into initializing colorlog's ColoredFormatter as **kwargsDictionary(*) colorlog's default (reference:https://github.com/borntyping/python-colorlog). . .There are many other options, see sl4p_examples in this branch.Make logging1. Load your configurationIt is important to load your configuration before using your application.
I recommend placing this loading config phase at the beginning of your application's main function.
Please note that this loading phase only needs to be executed once for the entire application package.When loading your configuration, you can pass your config (dict or Yaml file's path) as second parameter of getLogger().
Additionally, you can use the optional parameter debugprt=True to display sl4p's initialization info.Here are three ways to load your configuration:A.'get-logger' style loadinglog=sl4p.getLogger(__file__,cfg="./applog.cfg",debugprt=True)#debugprt param is optional!log.info("hello sl4p!")B.'with-block' style loadingwithsl4p(__file__,cfg=logcfg_dict)aslog:log.info("hello sl4p!")log.error("Error occured! #%d"%(400))# log.error("Error occured! ", 400)  --> Not support this 'print()' style, Please formatting.C.How to load logger in Jupyter__file__, the first argument of getLogger, is used as an identifier in Snippet logging.Instead, enter any string in Jupyter.log=sl4p.getLogger('jupyter',cfg=logcfg_dict):log.info('Hello sl4p in jupyter!')2. Write a log messageAfter loading your configuration, you can log messages anywhere in your application, even in another file or modules, without passing the second parameter into getLogger.Here are two ways to use logging:A.'get-logger' style logginglog=sl4p.getLogger(__file__)log.debug("Just pass only __file__")B.'with-block' style loggingwithsl4p(__file__)aslog:log.warning("warn msg! : you should not pass 2nd parameter to sl4p() after configuration completed")That's it! Enjoy logging your application :)Remember to load your configuration only once (initial entry-point, recommended) and log your messages everywhere !Utils & AdvancedThe 'with-block' logging feature can measure elapsed time as DEBUG log and can also include a custom tag usingtag={TAG_NAME}.Here is an example:importtimewithsl4p(__file__,tag='myTag')aslog:time.sleep(1.0)log.info('with-block time measuring and tagging feature!')This is the output log:DEBUG  | 2019.11.14-22:03:43 - example2.py #myTag @adfd08e4 started
INFO   | 2019.11.14-22:03:44 - with-block time measuring and tagging feature!
DEBUG  | 2019.11.14-22:03:44 - example2.py #myTag @adfd08e4 finished  ----  Elapsed    1.0001 sDecorator - @sl4p_time(tag={TAG_NAME})You can also use the@l4p_time(tag={TAG_NAME})decorator to measure the execution time of a function and log it with DEBUG level:@sl4p_time()# Although you do not use tagging, l4p_time decorator must be written with () brackets.defmy_function1():time.sleep(1.5)log.error('not yet...')This is the output log:DEBUG  | 2019.11.14-22:12:43 - example3.py f`my_function1() @19f19de9 started
ERROR  | 2019.11.14-22:12:44 - not yet...
DEBUG  | 2019.11.14-22:12:44 - example3.py f`my_function1 @19f19de9 finished  ----  Elapsed    1.5038 sDecorator - @sl4p_try, @sl4p_try_exityou can use the@sl4p_tryand@sl4p_try_exitdecorator to try-except your function and log the traceback of the exception.@sl4p_trydeftake_exception():a='ABCD'+NoneThis is the output log:ERROR  | 2019.11.14-22:12:44 - can only concatenate str (not "NoneType") to str
Traceback (most recent call last):
  File "D:\Devel_Workspace\py27_9\devel_logger\sl4p\decorators.py", line 21, in wrapped
    return func(*args, **kwargs)
  File "D:/Devel_Workspace/py27_9/devel_logger/example3.py", line 15, in take_exception
    a = 'ABCD' + None
TypeError: can only concatenate str (not "NoneType") to strRemember@sl4p_trywill continue the program when excepted, but@sl4p_try_exitwill terminate it.AboutDependenciesjsonpsutilcolorlogpywin32(only in windows)colorama(only in windows)LicenseBSD 3Getting Help / Discussion / ContributingRight here, please be active in this GitHub repository.All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.Release Note(*) ver 1.4.3 :: Prevented logfile name starting with . (period)ver 1.4.2 :: Coloring console log (by colorlog) and add related configs, Support console stdout'ver 1.4.1 :: Correct the docs, Change default config (console config has no initial values)ver 1.4.0 :: Layered apps-config, Support console level&format, IndLogger, Enhance stabilityver 1.3.3linux :: excluding 'psutil' in requires for linuxver 1.3.3 :: override_dict bugfix (support python 3.9+)ver 1.3.2 :: Add log_level for SimpleTimer: logger.create_simpleTimer(), config bugfixver 1.3.1 :: Add log_level for @sl4p_time decoratorver 1.2.0 :: Add stopwatch(SimpleTimer), Profiling stats(CPU & MEM), Recording TimeZonever 1.0.1 :: Supports multi-byte msg, update config and constants / Patch debugprt, start_tsver 1.0.0 :: First deployed release version