Skip to main content

Powerful functional logger with support for qt programming

Project description

template GitHub license

CHANGELOG CONTRIBUTING CODE_OF_CONDUCT PULL_REQUEST_TEMPLATE

Qt_Сolored-logger

Content

Preamble

I often came across the opinion that it is better to use not standard output to the console, but full-fledged logging... However, the standard libraries do not provide exactly what I need... Therefore, I decided to make my own library! Which will implement the functionality I need.

Overview

The library implements the formation of a beautifully formatted colored text, similar to a log, which has all the necessary information:

  • Logging time
  • Name of device and profile that logged
  • Log status
  • Description of the log status
  • Log type
  • Log message

Any information to the output can be turned off (according to the standard, everything is included). It is also possible to change the output settings during the logging process. It is possible to change colors (class AnsiColorSetInit and HtmlColorSetInitQ).

LICENSE

The full text of the license can be found at the following link.

Copyright © 2023 Kalynovsky Valentin. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and

Installation

Despite the fact that the library was originally developed for use in PyQt, it does not require PyQt to be installed, since this framework for outputting to Text fields, which support not only Plain Text, uses HTML and this library simply simplifies the logging process, since the creation process already formatted strings is registered in this library.

However, it is possible that this will be changed in the future, as the plans are to implement not just the formation of formatted strings, but to fully control the text flow in the console or text fields.

To install the library, enter the command:

pip install qt-colored-logger

Usage in console

The library has a complete X11 color table. However, logger use their own color tables for themselves, where the names of colors are determined not by its real physical name, but by a virtual one formed from the place where this color is used. These tables are initially empty. To initialize them, you need to create an object of the AnsiColorSetInit class, which, in addition to filling the table, provides methods for changing colors in the table. After that, you can already use the color table, and therefore you can start the logging process.

Since the library is under active development, not the best solutions have been applied at this stage, which will be corrected in the future. But at the moment it is NOT RECOMMENDED to name an object of class Logger by the name log!

Logging is done by the LoggerQ class. To write to the log, you need to call a method with the desired entry type. There are 16 in total: see section Data/"Entry types".

Not only the log itself has settings, but also each type of record. However, the log settings apply to all entries. Therefore, if you need to disable the output of a specific part of the record for a specific type of record, this must be done before each output of this record to the log (i.e., disable the output before writing and turn it back on after the output, so that this part of the information would be displayed for other types). This approach is used only if the part is disabled only for one or more data types.

There are few settings for each entry type. There you can turn on/off only the text format bold/italic/standard. Also, do not forget to pass the status text (if enabled) and the message text (if enabled) to the record, since the developer himself determines the data to be recorded.

Here is an example using the library:

from qt_colored_logger import AnsiColorSetInit, Logger

if __name__ == '__main__':
    color = AnsiColorSetInit()
    logger = Logger(status_message=False)
    print(logger.DEBUG(message_text="Debug data"))
    print(logger.DEBUG(message_text="Debug data", bold=True))
    print(logger.DEBUG(message_text="Debug data", italic=True))
    print(logger.DEBUG(message_text="Debug data", bold=True, italic=True))

The outputs in console will contain the following text (GitHub, PyPi and possibly some other sites do not support displaying colors in Markdown - use resources that support them, such as PyCharm):

*2023-03-26 13:25:58.091911 $DESKTOP-NUMBER^User #STATUS: @DEBUG - Debug data
*2023-03-26 13:25:58.093911 $DESKTOP-NUMBER^User #STATUS: @DEBUG - Debug data
*2023-03-26 13:25:58.093911 $DESKTOP-NUMBER^User #STATUS: @DEBUG - Debug data
*2023-03-26 13:25:58.093911 $DESKTOP-NUMBER^User #STATUS: @DEBUG - Debug data

If you want to change the color of a part of a post, you need to refer to the logger's color chart. The class Logger has read access, and AnsiColorSetInit has write access. As already mentioned, AnsiColorSetInit not only forms a table, but also provides methods for changing colors. The Logger Color Chart has the following color names: see section Data/"Logger Color Chart".

Here is an example of a color change:

from qt_colored_logger import AnsiColorSetInit, Logger

if __name__ == '__main__':
    color = AnsiColorSetInit()
    logger = Logger(status_message=False)
    print(logger.NOTICE(message_text="Notice data"))
    color.setColor("NOTICE_MESSAGE", [127, 255, 0])
    print(logger.NOTICE(message_text="Notice data"))

The outputs in console will contain the following text (GitHub, PyPi and possibly some other sites do not support displaying colors in Markdown - use resources that support them, such as PyCharm):

*2023-03-26 13:52:29.519001 $DESKTOP-NUMBER^User #STATUS: @NOTICE - Notice data
*2023-03-26 13:52:29.519001 $DESKTOP-NUMBER^User #STATUS: @NOTICE - Notice data

This is the simplest example of using the library:

from qt_colored_logger import AnsiColorSetInit, Logger

if __name__ == "__main__":
	mod = AnsiColorSetInit()
	logger = Logger()
	print(logger.DEBUG("1", "2"))
	print(logger.DEBUG_PERFORMANCE("3", "4"))
	print(logger.PERFORMANCE("5", "6"))
	print(logger.EVENT("7", "8"))
	print(logger.AUDIT("9", "10"))
	print(logger.METRICS("11", "12"))
	print(logger.USER("13", "14"))
	print(logger.MESSAGE("15", "16"))
	print(logger.INFO("17", "18"))
	print(logger.NOTICE("19", "20"))
	print(logger.WARNING("21", "22"))
	print(logger.ERROR("23", "24"))
	print(logger.CRITICAL("25", "26"))
	# print(logger.START_PROCESS("27", "28"))
	print(logger.SUCCESS("29", "30"))
	print(logger.FAIL("31", "32"))

The outputs in console will contain the following text (GitHub, PyPi and possibly some other sites do not support displaying colors in Markdown - use resources that support them, such as PyCharm):

*2023-03-26 13:54:25.837031 $DESKTOP-NUMBER^User #STATUS: 1 @DEBUG - 2
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 3 @DEBUG PERFORMANCE - 4
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 5 @PERFORMANCE - 6
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 7 @EVENT - 8
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 9 @AUDIT - 10
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 11 @METRICS - 12
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 13 @USER - 14
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 15 @MESSAGE - 16
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 17 @INFO - 18
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 19 @NOTICE - 20
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 21 @WARNING - 22
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 23 !ERROR - 24
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 25 !!!@CRITICAL - 26
*2023-03-26 13:54:25.869034 $DESKTOP-NUMBER^User #STATUS: 29 @SUCCESS - 30
*2023-03-26 13:54:25.871033 $DESKTOP-NUMBER^User #STATUS: 31 @FAIL - 32

Usage in Qt

Разница между консолью и Qt в том, что Qt использует HTML для отображения форматированого текста, а консоль - ANSI escape code. Однако к базовой логике доступ не планируется, а внешний интерфейс между классами AnsiColorSetInit/HtmlColorSetInitQ и Logger/LoggerQ не отличается (кроме самих названий классов). Поэтому все преддыдущие примеры можно переписать так:

from qt_colored_logger import HtmlColorSetInitQ, LoggerQ

...

self.color = HtmlColorSetInitQ()
self.logger = LoggerQ(status_message=False)
self.someTextBrowserObject.append(logger.DEBUG(message_text="Debug data"))
self.someTextBrowserObject.append(logger.DEBUG(message_text="Debug data", bold=True))
self.someTextBrowserObject.append(logger.DEBUG(message_text="Debug data", italic=True))
self.someTextBrowserObject.append(logger.DEBUG(message_text="Debug data", bold=True, italic=True))

...
from qt_colored_logger import HtmlColorSetInitQ, LoggerQ

...

color = HtmlColorSetInitQ()
logger = LoggerQ(status_message=False)
self.someTextBrowserObject.append(logger.NOTICE(message_text="Notice data"))
color.setColor("NOTICE_MESSAGE", [127, 255, 0])
self.someTextBrowserObject.append(logger.NOTICE(message_text="Notice data"))

...
from qt_colored_logger import HtmlColorSetInitQ, LoggerQ

...

mod = HtmlColorSetInitQ()
logger = LoggerQ()
self.someTextBrowserObject.append(logger.DEBUG("1", "2"))
self.someTextBrowserObject.append(logger.DEBUG_PERFORMANCE("3", "4"))
self.someTextBrowserObject.append(logger.PERFORMANCE("5", "6"))
self.someTextBrowserObject.append(logger.EVENT("7", "8"))
self.someTextBrowserObject.append(logger.AUDIT("9", "10"))
self.someTextBrowserObject.append(logger.METRICS("11", "12"))
self.someTextBrowserObject.append(logger.USER("13", "14"))
self.someTextBrowserObject.append(logger.MESSAGE("15", "16"))
self.someTextBrowserObject.append(logger.INFO("17", "18"))
self.someTextBrowserObject.append(logger.NOTICE("19", "20"))
self.someTextBrowserObject.append(logger.WARNING("21", "22"))
self.someTextBrowserObject.append(logger.ERROR("23", "24"))
self.someTextBrowserObject.append(logger.CRITICAL("25", "26"))
# self.someTextBrowserObject.append(logger.START_PROCESS("27", "28"))
self.someTextBrowserObject.append(logger.SUCCESS("29", "30"))
self.someTextBrowserObject.append(logger.FAIL("31", "32"))

...

Additional functionality

Additional functionality is also planned. Let's keep it a secret for now. Let it be a surprise.

Data

The library stores various important data for use that you may need to know while using the library.

Entry types:
  • DEBUG
  • DEBUG_PERFORMANCE
  • PERFORMANCE
  • EVENT
  • AUDIT
  • METRICS
  • USER
  • MESSAGE
  • INFO
  • NOTICE
  • WARNING
  • ERROR
  • CRITICAL
  • PROGRESS (not implemented because the non-implemented START_PROCESS and STOP_PROCESS methods control this type)
  • SUCCESS
  • FAIL
X11 color table:
  • Red category:
    • MAROON
    • DARKRED
    • RED
    • LIGHTRED
    • FIREBRICK
    • CRIMSON
    • INDIANRED
    • LIGHTCORAL
    • SALMON
    • DARKSALMON
    • LIGHTSALMON
  • Pink category:
    • MEDIUMVIOLETRED
    • DEEPPINK
    • PALEVIOLETRED
    • HOTPINK
    • LIGHTPINK
    • PINK
  • Orange category:
    • ORANGERED
    • TOMATO
    • DARKORANGE
    • CORAL
    • ORANGE
  • Yellow category:
    • DARKKHAKI
    • GOLD
    • KHAKI
    • PEACHPUFF
    • YELLOW
    • DARKYELLOW
    • PALEGOLDENROD
    • MOCCASIN
  • Purple category:
    • INDIGO
    • PURPLE
    • DARKMAGENTA
    • DARKVIOLET
    • DARKSLATEBLUE
    • BLUEVIOLET
    • DARKORCHID
    • FUCHSIA
    • SLATEBLUE
    • MEDIUMSLATEBLUE
    • MEDIUMORCHID
    • MEDIUMPURPLE
    • ORCHID
    • VIOLET
    • PLUM
    • THISTLE
    • LAVENDER
  • Green category:
    • DARKGREEN
    • GREEN
    • DARKOLIVEGREEN
    • FORESTGREEN
    • SEAGREEN
    • DARKSLATEGRAY
    • OLIVE
    • OLIVEDRAB
    • MEDIUMSEAGREEN
    • LIMEGREEN
    • LIME
    • SPRINGGREEN
    • MEDIUMSPRINGGREEN
    • DARKSEAGREEN
    • MEDIUMAQUAMARINE
    • YELLOWGREEN
    • LAWNGREEN
    • CHARTREUSE
    • LIGHTGREEN
    • GREENYELLOW
    • PALEGREEN
  • Aqua category:
    • TEAL
    • DARKCYAN
    • LIGHTSEAGREEN
    • CADETBLUE
    • DARKTURQUOISE
    • MEDIUMTURQUOISE
    • TURQUOISE
    • AQUA
    • AQUAMARINE
    • SKYBLUE
    • LIGHTSKYBLUE
    • LIGHTSTEELBLUE
    • LIGHTBLUE
    • POWDERBLUE
    • PALETURQUOISE
  • Blue category:
    • MIDNIGHTBLUE
    • NAVY
    • DARKBLUE
    • MEDIUMBLUE
    • BLUE
    • ROYALBLUE
    • STEELBLUE
    • DODGERBLUE
    • DEEPSKYBLUE
    • CORNFLOWERBLUE
  • Brown category:
    • BROWN
    • SADDLEBROWN
    • SIENNA
    • CHOCOLATE
    • DARKGOLDENROD
    • PERU
    • ROSYBROWN
    • GOLDENROD
    • SANDYBROWN
    • TAN
    • BURLYWOOD
    • WHEAT
    • NAVAJOWHITE
    • BISQUE
    • BLANCHEDALMOND
  • White category:
    • WHITE
    • SNOW
    • HONEYDEW
    • MINTCREAM
    • AZURE
    • LIGHTCYAN
    • ALICEBLUE
    • GHOSTWHITE
    • WHITESMOKE
    • SEASHELL
    • BEIGE
    • OLDLACE
    • FLORALWHITE
    • IVORY
    • ANTIQUEWHITE
    • LINEN
    • LAVENDERBLUSH
    • MISTYROSE
    • PAPAYAWHIP
    • LIGHTGOLDENRODYELLOW
    • CORNSILK
    • LEMONCHIFFON
    • LIGHTYELLOW
  • Gray and black category:
    • BLACK
    • DARKGRAY
    • DIMGRAY
    • SLATEGRAY
    • GRAY
    • LIGHTSLATEGRAY
    • SILVER
    • LIGHTGRAY
    • GAINSBORO
Default Color Scheme:
  • ORCHID
  • MEDIUMORCHID
  • ORANGE
  • DARKORANGE
  • BURLYWOOD
  • TAN
  • NAVAJOWHITE
  • WHEAT
  • BLANCHEDALMOND
  • BISQUE
  • MEDIUMSEAGREEN
  • SEAGREEN
  • YELLOWGREEN
  • OLIVEDRAB
  • OLIVE
  • DARKOLIVEGREEN
  • PALEGREEN
  • LIGHTGREEN
  • LIGHTSTEELBLUE
  • POWDERBLUE
  • PALETURQUOISE
  • LIGHTBLUE
  • DEEPSKYBLUE
  • DODGERBLUE
  • YELLOW
  • DARKYELLOW
  • FIREBRICK
  • DARKRED
  • MAROON
  • SKYBLUE
  • LIGHTSKYBLUE
  • GREEN
  • DARKGREEN
Logger Color Chart:
  • TIME
  • USER
  • STATUS
  • STATUS_MESSAGE
  • TYPE_DEBUG
  • DEBUG_MESSAGE
  • TYPE_DEBUG_PERFORMANCE
  • DEBUG_PERFORMANCE_MESSAGE
  • TYPE_PERFORMANCE
  • PERFORMANCE_MESSAGE
  • TYPE_EVENT
  • EVENT_MESSAGE
  • TYPE_AUDIT
  • AUDIT_MESSAGE
  • TYPE_METRICS
  • METRICS_MESSAGE
  • TYPE_USER
  • USER_MESSAGE
  • TYPE_MESSAGE
  • MESSAGE_MESSAGE
  • TYPE_INFO
  • INFO_MESSAGE
  • TYPE_NOTICE
  • NOTICE_MESSAGE
  • TYPE_WARNING
  • WARNING_MESSAGE
  • TYPE_ERROR
  • ERROR_MESSAGE
  • TYPE_CRITICAL
  • CRITICAL_MESSAGE
  • TYPE_PROGRESS
  • PROGRESS_MESSAGE
  • TYPE_SUCCESS
  • SUCCESS_MESSAGE
  • TYPE_FAIL
  • FAIL_MESSAGE
Tree of ANSI escape code:
  • reset

    • on
  • bold

    • on
    • off (doubly underlined)
  • faint

    • on
    • off
  • italic

    • on
    • fraktur
    • off
  • underline

    • on
    • off
  • blink

    • slow
    • rapid
    • off
  • proportional spacing

    • on
    • off
  • invert

    • on
    • off
  • hide

    • on
    • off
  • strike

    • on
    • off
  • over line

    • on
    • off
  • framed

    • on
    • encircled
    • off
  • font

    • primary
    • 1st alternative
    • 2nd alternative
    • 3rd alternative
    • 4th alternative
    • 5th alternative
    • 6th alternative
    • 7th alternative
    • 8th alternative
    • 9th alternative
  • color

    • foreground
      • black
      • red
      • green
      • yellow
      • blue
      • magenta
      • cyan
      • white
    • background
      • black
      • red
      • green
      • yellow
      • blue
      • magenta
      • cyan
      • white
    • bright foreground
      • black
      • red
      • green
      • yellow
      • blue
      • magenta
      • cyan
      • white
    • bright background
      • black
      • red
      • green
      • yellow
      • blue
      • magenta
      • cyan
      • white
    • set
      • foreground
        • R;G;B
      • background
        • R;G;B
      • bright foreground
        • R;G;B
      • bright background
        • R;G;B
      • underline
        • R;G;B
    • default
      • foreground
      • background
      • bright foreground
      • bright background
      • underline
  • Content

Troubleshooting

All functionality of the library has been tested by me, but if you have problems using it, the code does not work, have suggestions for optimization or advice for improving the style of the code and the name - I invite you here and here.

Authors


Kalynovsky Valentin

"Ideological inspirer and Author"

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

qt_colored_logger-0.2.1.tar.gz (27.4 kB view hashes)

Uploaded Source

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