Package: lovely-logger
lovely_loggerA logger library which builds on, combines, and simplifies various logging features of Python 3.lovely_logger is a highly robust, production-ready, feature-rich logger which is used throughout theTier2 Technologiessoftware stackWhy using lovely_logger is better than using the built-in python logging module:It is much easier to useIt handles automatic logging of uncaught exception tracebacksIt is thread-safeIt will flush the log to disk on program crash/exitIt handles simultaneous logging to both stdout (the console) and automatically rotating log files by defaultIt supports independent formatters for console vs files out-of-the-boxIt supports both timezones and milliseconds in the timestampIt has sane defaults, like logging a timestamp in the first placeIt supports shorthand which makes it just as quick to log as it is toprint()Getting Started:importlovely_loggeraslog# pip install lovely-loggerlog.init('filename.log')log.info('Hello World!')It's that easy!Another Example:importlovely_loggerasloglog.init('./my_log_file.log')log.debug('here are the in-scope variables right now:%s',dir())log.info('%sv1.2 HAS STARTED',__file__)log.warning('here is a warning message')log.error('generally you would use error for handled exceptions which prevent further execution')log.critical('generally you would use critical for uncaught exceptions')Theinit()function has more optional parameters:init(filename,to_console=True,level=DEBUG,max_kb=1024,max_files=5)Settingto_consoletoFalseis useful for windowed applications such as those compiled withpyinstallerwhich have no console.The valid options forlevelareDEBUG,INFO,WARNING,ERROR, andCRITICALin that order. setting the level toERROR, for example, will silence yourlog.debug()andlog.warning()calls while still logging yourlog.error()andlog.critical()calls.max_kbsets the max logfile size before the log is rotatedmax_filessets the max number of rotating logs that are to be kept before the oldest is deleted. So, for example, the defaultmax_kbof 1024 andmax_filesof 5 means that up to 5 megabytes of logs will be kept, split among five 1MB files. Once the log reaches 5MB and 1byte, the oldest of the 5 files is rotated away, leaving four 1MB archived log files, and a 1byte active log fileThere is another special type of log function that can only be used inside of an exception handler. It will log the full exception traceback for you, (as level=CRITICAL) along with any helpful comments you may have about the exceptiontry:a=1/0except:log.exception("You can't divide by zero!")because nobody has time to type out complicated words likeexceptionorcritical, and code looks worse when the print statements are all different lengths, there is shorthand here for you.instead of this:importlovely_loggerasloglog.init('my_log_file.log')log.debug('This is a debug log entry')log.info('This is a info log entry')log.warning('This is a warning log entry')log.error('This is a error log entry')log.critical('This is a critical log entry')try:a=1/0except:log.exception('This is an exception log entry')you can write it like this:importlovely_loggerasloglog.init('my_log_file.log')log.d('This is a debug log entry')log.i('This is a info log entry')log.w('This is a warning log entry')log.e('This is a error log entry')log.c('This is a critical log entry')try:a=1/0except:log.x('This is an exception log entry')By default, the logger is going to output the date/time, level, message, filename, and line number into the log file. It will print all of that same info except the date/time to the console. If you want to override what gets outputted, or change the format, you can manually set the formatting:importlovely_loggerasloglog.FILE_FORMAT="[%(asctime)s] [%(levelname)-8s] -%(message)s(%(filename)s:%(lineno)s)"log.CONSOLE_FORMAT="[%(levelname)-8s] -%(message)s(%(filename)s:%(lineno)s)"log.DATE_FORMAT='%Y-%m-%d%H:%M:%S.uuu%z'log.init('my_log_file.log')log.d('This is a debug log entry')DATE_FORMATfollows the formatting of the built in pythontime.strftime()with the exception of the "uuu" which was added to support millisecondsCONSOLE_FORMATandFILE_FORMATfollow the formatting from the built in python logging libraryLogRecord attributesNote that those values must be set beforelog.init()is called.