{"id":329577,"date":"2024-05-15T14:08:17","date_gmt":"2024-05-15T14:08:17","guid":{"rendered":"https:\/\/www.blog.pythonlibrary.org\/?p=12333"},"modified":"2024-05-15T14:08:17","modified_gmt":"2024-05-15T14:08:17","slug":"an-intro-to-logging-with-python-and-loguru","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2024\/05\/15\/an-intro-to-logging-with-python-and-loguru\/","title":{"rendered":"An Intro to Logging with Python and Loguru"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p dir=\"auto\">Python&#8217;s\u00a0<code>logging<\/code>\u00a0module isn&#8217;t the only way to create logs. There are several third-party packages you can use, too. One of the most popular is\u00a0<a href=\"https:\/\/github.com\/Delgan\/loguru\">Loguru<\/a>. Loguru intends to remove all the boilerplate you get with the Python logging API.<\/p>\n<p dir=\"auto\">You will find that Loguru greatly simplifies creating logs in Python.<\/p>\n<p dir=\"auto\">This chapter has the following sections:<\/p>\n<ul dir=\"auto\">\n<li>Installation<\/li>\n<li>Logging made simple<\/li>\n<li>Handlers and formatting<\/li>\n<li>Catching exceptions<\/li>\n<li>Terminal logging with color<\/li>\n<li>Easy log rotation<\/li>\n<\/ul>\n<p dir=\"auto\">Let&#8217;s find out how much easier Loguru makes logging in Python!<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Installation<\/h2>\n<p><a id=\"user-content-installation\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#installation\" aria-label=\"Permalink: Installation\"><\/a><\/div>\n<p dir=\"auto\">Before you can start with Loguru, you will need to install it. After all, the Loguru package doesn&#8217;t come with Python.<\/p>\n<p dir=\"auto\">Fortunately, installing Loguru is easy with pip. Open up your terminal and run the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">python -m pip install loguru<\/pre>\n<p dir=\"auto\">Pip will install Loguru and any dependencies it might have for you. You will have a working package installed if you see no errors.<\/p>\n<p dir=\"auto\">Now let&#8217;s start logging!<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Logging Made Simple<\/h2>\n<p><a id=\"user-content-logging-made-simple\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#logging-made-simple\" aria-label=\"Permalink: Logging Made Simple\"><\/a><\/div>\n<p dir=\"auto\">Logging with Loguru can be done in two lines of code. Loguru is really that simple!<\/p>\n<p dir=\"auto\">Don&#8217;t believe it? Then open up your Python IDE or REPL and add the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># hello.py\r\n\r\nfrom loguru import logger\r\n\r\nlogger.debug(\"Hello from loguru!\")\r\nlogger.info(\"Informed from loguru!\")<\/pre>\n<p dir=\"auto\">One import is all you need. Then, you can immediately start logging! By default, the log will go to stdout.<\/p>\n<p dir=\"auto\">Here&#8217;s what the output looks like in the terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">2024-05-07 14:34:28.663 | DEBUG    | __main__:&lt;module&gt;:5 - Hello from loguru!\r\n2024-05-07 14:34:28.664 | INFO     | __main__:&lt;module&gt;:6 - Informed from loguru!<\/pre>\n<p dir=\"auto\">Pretty neat! Now, let&#8217;s find out how to change the handler and add formatting to your output.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Handlers and Formatting<\/h2>\n<p><a id=\"user-content-handlers-and-formatting\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#handlers-and-formatting\" aria-label=\"Permalink: Handlers and Formatting\"><\/a><\/div>\n<p dir=\"auto\">Loguru doesn&#8217;t think of handlers the way the Python logging module does. Instead, you use the concept of\u00a0<code>sinks<\/code>. The\u00a0<a href=\"https:\/\/loguru.readthedocs.io\/en\/stable\/api\/logger.html#sink\" rel=\"nofollow\">sink<\/a>\u00a0tells Loguru how to handle an incoming log message and write it somewhere.<\/p>\n<p dir=\"auto\">Sinks can take lots of different forms:<\/p>\n<ul dir=\"auto\">\n<li>A file-like object, such as\u00a0<code>sys.stderr<\/code>\u00a0or a file handle<\/li>\n<li>A file path as a string or\u00a0<code>pathlib.Path<\/code><\/li>\n<li>A\u00a0<code>callable<\/code>, such as a simple function<\/li>\n<li>An asynchronous\u00a0<code>coroutine function<\/code>\u00a0that you define using\u00a0<code>async def<\/code><\/li>\n<li>A built-in\u00a0<code>logging.Handler<\/code>. If you use these, the Loguru records convert to\u00a0<code>logging<\/code>\u00a0records automatically<\/li>\n<\/ul>\n<p dir=\"auto\">To see how this works, create a new file called\u00a0<code>file_formatting.py<\/code>\u00a0in your Python IDE. Then add the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># file_formatting.py\r\n\r\nfrom loguru import logger\r\n\r\nfmt = \"{time} - {name} - {level} - {message}\"\r\n\r\nlogger.add(\"formatted.log\", format=fmt, level=\"INFO\")\r\nlogger.debug(\"This is a debug message\")\r\nlogger.info(\"This is an informational message\")<\/pre>\n<p dir=\"auto\">If you want to change where the logs go, use the\u00a0<code>add()<\/code>\u00a0method. Note that this adds a new sink, which, in this case, is a file. The logger will still log to stdout, too, as that is the default, and you are adding to the handler list. If you want to remove the default sink, add\u00a0<code>logger.remove()<\/code>\u00a0before you call\u00a0<code>add()<\/code>.<\/p>\n<p dir=\"auto\">When you call\u00a0<code>add()<\/code>, you can pass in several different arguments:<\/p>\n<ul dir=\"auto\">\n<li>sink &#8211; Where to send the log messages<\/li>\n<li>level &#8211; The logging level<\/li>\n<li>format &#8211; How to format the log messages<\/li>\n<li>filter &#8211; A logging filter<\/li>\n<\/ul>\n<p dir=\"auto\">There are several more, but those are the ones you would use the most. If you want to know more about\u00a0<code>add()<\/code>, you should check out the\u00a0<a href=\"https:\/\/loguru.readthedocs.io\/en\/stable\/api\/logger.html\" rel=\"nofollow\">documentation<\/a>.<\/p>\n<p dir=\"auto\">You might have noticed that the formatting of the log records is a little different than what you saw in Python&#8217;s own\u00a0<code>logging<\/code>\u00a0module.<\/p>\n<p dir=\"auto\">Here is a listing of the formatting directives you can use for Loguru:<\/p>\n<ul dir=\"auto\">\n<li>elapsed &#8211; The time elapsed since the app started<\/li>\n<li>exception &#8211; The formatted exception, if there was one<\/li>\n<li>extra &#8211; The dict of attributes that the user bound<\/li>\n<li>file &#8211; The name of the file where the logging call came from<\/li>\n<li>function &#8211; The function where the logging call came from<\/li>\n<li>level &#8211; The logging level<\/li>\n<li>line &#8211; The line number in the source code<\/li>\n<li>message &#8211; The unformatted logged message<\/li>\n<li>module &#8211; The module that the logging call was made from<\/li>\n<li>name &#8211; The\u00a0<code>__name__<\/code>\u00a0where the logging call came from<\/li>\n<li>process &#8211; The process in which the logging call was made<\/li>\n<li>thread &#8211; The thread in which the logging call was made<\/li>\n<li>time &#8211; The aware local time when the logging call was made<\/li>\n<\/ul>\n<p dir=\"auto\">You can also change the time formatting in the logs. In this case, you would use a subset of the formatting from the\u00a0<a href=\"https:\/\/pendulum.eustace.io\/docs\/#tokens\" rel=\"nofollow\">Pendulum package<\/a>. For example, if you wanted to make the time exclude the date, you would use this:\u00a0<code>{time:HH:mm:ss}<\/code>\u00a0rather than simply\u00a0<code>{time}<\/code>, which you see in the code example above.<\/p>\n<p dir=\"auto\">See the\u00a0<a href=\"https:\/\/loguru.readthedocs.io\/en\/stable\/api\/logger.html#record\" rel=\"nofollow\">documentation<\/a>\u00a0for details on formating time and messages.<\/p>\n<p dir=\"auto\">When you run the code example, you will see something similar to the following in your log file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">2024-05-07T14:35:06.553342-0500 - __main__ - INFO - This is an informational message<\/pre>\n<p dir=\"auto\">You will also see log messages sent to your terminal in the same format as you saw in the first code example.<\/p>\n<p dir=\"auto\">Now, you&#8217;re ready to move on and learn about catching exceptions with Loguru.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Catching Exceptions<\/h2>\n<p><a id=\"user-content-catching-exceptions\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#catching-exceptions\" aria-label=\"Permalink: Catching Exceptions\"><\/a><\/div>\n<p dir=\"auto\">Catching exceptions with Loguru is done by using a decorator. You may remember that when you use Python&#8217;s own\u00a0<code>logging<\/code>\u00a0module, you use\u00a0<code>logger.exception<\/code>\u00a0in the\u00a0<code>except<\/code>\u00a0portion of a\u00a0<code>try\/except<\/code>\u00a0statement to record the exception&#8217;s traceback to your log file.<\/p>\n<p dir=\"auto\">When you use Loguru, you use the\u00a0<code>@logger.catch<\/code>\u00a0decorator on the function that contains code that may raise an exception.<\/p>\n<p dir=\"auto\">Open up your Python IDE and create a new file named\u00a0<code>catching_exceptions.py<\/code>. Then enter the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># catching_exceptions.py\r\n\r\nfrom loguru import logger\r\n\r\n@logger.catch\r\ndef silly_function(x, y, z):\r\n    return 1 \/ (x + y + z)\r\n\r\ndef main():\r\n    fmt = \"{time:HH:mm:ss} - {name} - {level} - {message}\"\r\n    logger.add(\"exception.log\", format=fmt, level=\"INFO\")\r\n    logger.info(\"Application starting\")\r\n    silly_function(0, 0, 0)\r\n    logger.info(\"Finished!\")\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/pre>\n<p dir=\"auto\">According to Loguru&#8217;s documentation, the&#8217; @logger.catch` decorator will catch regular exceptions and also work with applications with multiple threads. Add another file handler on top of the stream handler and start logging for this example.<\/p>\n<p dir=\"auto\">Then you call\u00a0<code>silly_function()<\/code>\u00a0with a bunch of zeroes, which causes a\u00a0<code>ZeroDivisionError<\/code>\u00a0exception.<\/p>\n<p dir=\"auto\">Here&#8217;s the output from the terminal:<\/p>\n<p dir=\"auto\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-large wp-image-12334\" src=\"https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-1024x481.png\" alt=\"Loguru Exception Handling\" width=\"1024\" height=\"481\" srcset=\"https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-1024x481.png 1024w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-300x141.png 300w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-768x361.png 768w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-1536x722.png 1536w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/exceptions-2048x963.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p dir=\"auto\">If you open up the\u00a0<code>exception.log<\/code>, you will see that the contents are a little different because you formatted the timestamp and also because logging those funny lines that show what arguments were passed to the\u00a0<code>silly_function()<\/code>\u00a0don&#8217;t translate that well:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">14:38:30 - __main__ - INFO - Application starting\r\n14:38:30 - __main__ - ERROR - An error has been caught in function 'main', process 'MainProcess' (8920), thread 'MainThread' (22316):\r\nTraceback (most recent call last):\r\n\r\n  File \"C:\\books\\11_loguru\\catching_exceptions.py\", line 17, in &lt;module&gt;\r\n    main()\r\n    \u00e2\u201d\u201d &lt;function main at 0x00000253B01AB7E0&gt;\r\n\r\n&gt; File \"C:\\books\\11_loguru\\catching_exceptions.py\", line 13, in main\r\n    silly_function(0, 0, 0)\r\n    \u00e2\u201d\u201d &lt;function silly_function at 0x00000253ADE6D440&gt;\r\n\r\n  File \"C:\\books\\11_loguru\\catching_exceptions.py\", line 7, in silly_function\r\n    return 1 \/ (x + y + z)\r\n                \u00e2\u201d\u201a   \u00e2\u201d\u201a   \u00e2\u201d\u201d 0\r\n                \u00e2\u201d\u201a   \u00e2\u201d\u201d 0\r\n                \u00e2\u201d\u201d 0\r\n\r\nZeroDivisionError: division by zero\r\n14:38:30 - __main__ - INFO - Finished!<\/pre>\n<p dir=\"auto\">On the whole, using the\u00a0<code>@logger.catch<\/code>\u00a0is a nice way to catch exceptions.<\/p>\n<p dir=\"auto\">Now, you&#8217;re ready to move on and learn about changing the color of your logs in the terminal.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Terminal Logging with Color<\/h2>\n<p><a id=\"user-content-terminal-logging-with-color\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#terminal-logging-with-color\" aria-label=\"Permalink: Terminal Logging with Color\"><\/a><\/div>\n<p dir=\"auto\">Loguru will print out logs in color in the terminal by default if the terminal supports color. Colorful logs can make reading through the logs easier as you can highlight warnings and exceptions with unique colors.<\/p>\n<p dir=\"auto\">You can use markup tags to add specific colors to any formatter string. You can also apply bold and underline to the tags.<\/p>\n<p dir=\"auto\">Open up your Python IDE and create a new file called\u00a0<code>terminal_formatting.py<\/code>. After saving the file, enter the following code into it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># terminal_formatting.py\r\nimport sys\r\nfrom loguru import logger\r\n\r\nfmt = (\"&lt;red&gt;{time}&lt;\/red&gt; - \"\r\n       \"&lt;yellow&gt;{name}&lt;\/yellow&gt; - \"\r\n       \"{level} - {message}\")\r\n\r\nlogger.add(sys.stdout, format=fmt, level=\"DEBUG\")\r\nlogger.debug(\"This is a debug message\")\r\nlogger.info(\"This is an informational message\")<\/pre>\n<p dir=\"auto\">You create a special format that sets the &#8220;time&#8221; portion to red and the &#8220;name&#8221; to yellow. Then, you <code>add()<\/code>\u00a0that format to the logger. You will now have two sinks: the default root handler, which logs to stderr, and the new sink, which logs to stdout. You do formatting to compare the default colors to your custom ones.<\/p>\n<p dir=\"auto\">Go ahead and run the code. You should see something like this:<\/p>\n<p dir=\"auto\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-12335\" src=\"https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal-1024x184.png\" alt=\"Changing terminal output colors with Loguru\" width=\"1024\" height=\"184\" srcset=\"https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal-1024x184.png 1024w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal-300x54.png 300w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal-768x138.png 768w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal-1536x276.png 1536w, https:\/\/www.blog.pythonlibrary.org\/wp-content\/uploads\/2024\/05\/terminal.png 1948w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p dir=\"auto\">Neat! It would be best if you now spent a few moments studying the\u00a0<a href=\"https:\/\/loguru.readthedocs.io\/en\/stable\/api\/logger.html#color\" rel=\"nofollow\">documentation<\/a>\u00a0and trying out some of the other colors. For example, you can use hex and RGB colors and a handful of named colors.<\/p>\n<p dir=\"auto\">The last section you will look at is how to do log rotation with Loguru!<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Easy Log Rotation<\/h2>\n<p><a id=\"user-content-easy-log-rotation\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#easy-log-rotation\" aria-label=\"Permalink: Easy Log Rotation\"><\/a><\/div>\n<p dir=\"auto\">Loguru makes log rotation easy. You don&#8217;t need to import any special handlers. Instead, you only need to specify the\u00a0<code>rotation<\/code>\u00a0argument when you call\u00a0<code>add()<\/code>.<\/p>\n<p dir=\"auto\">Here are a few examples:<\/p>\n<ul dir=\"auto\">\n<li><code>logger.add(\"file.log\", rotation=\"100 MB\")<\/code><\/li>\n<li><code>logger.add(\"file.log\", rotation=\"12:00\")<\/code><\/li>\n<li><code>logger.add(\"file.log\", rotation=\"1 week\")<\/code><\/li>\n<\/ul>\n<p dir=\"auto\">These demonstrate that you can set the rotation at 100 megabytes at noon daily or even rotate weekly.<\/p>\n<p dir=\"auto\">Open up your Python IDE so you can create a full-fledged example. Name the file\u00a0<code>log_rotation.py<\/code>\u00a0and add the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># log_rotation.py\r\n\r\nfrom loguru import logger\r\n\r\nfmt = \"{time} - {name} - {level} - {message}\"\r\n\r\nlogger.add(\"rotated.log\",\r\n           format=fmt,\r\n           level=\"DEBUG\",\r\n           rotation=\"50 B\")\r\nlogger.debug(\"This is a debug message\")\r\nlogger.info(\"This is an informational message\")<\/pre>\n<p dir=\"auto\">Here, you set up a log format, set the level to DEBUG, and set the rotation to every 50 bytes. When you run this code, you will get a couple of log files. Loguru will add a timestamp to the file&#8217;s name when it rotates the log.<\/p>\n<p dir=\"auto\">What if you want to add compression? You don&#8217;t need to override the rotator like you did with Python&#8217;s\u00a0<code>logging<\/code>\u00a0module. Instead, you can turn on compression using the\u00a0<code>compression<\/code>\u00a0argument.<\/p>\n<p dir=\"auto\">Create a new Python script called\u00a0<code>log_rotation_compression.py<\/code>\u00a0and add this code for a fully working example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># log_rotation_compression.py\r\n\r\nfrom loguru import logger\r\n\r\nfmt = \"{time} - {name} - {level} - {message}\"\r\n\r\nlogger.add(\"compressed.log\",\r\n           format=fmt,\r\n           level=\"DEBUG\",\r\n           rotation=\"50 B\",\r\n           compression=\"zip\")\r\nlogger.debug(\"This is a debug message\")\r\nlogger.info(\"This is an informational message\")\r\nfor i in range(10):\r\n    logger.info(f\"Log message {i}\")<\/pre>\n<p>The new file is automatically compressed in the zip format when the log rotates. There is also a\u00a0<code>retention<\/code>\u00a0argument that you can use with\u00a0<code>add()<\/code>\u00a0to tell Loguru to clean the logs after so many days:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">logger.add(\"file.log\",\r\n             rotation=\"100 MB\",\r\n             retention=\"5 days\")<\/pre>\n<p dir=\"auto\">If you were to add this code, the logs that were more than five days old would get cleaned up automatically by Loguru!<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\" tabindex=\"-1\">Wrapping Up<\/h2>\n<p><a id=\"user-content-wrapping-up\" class=\"anchor\" href=\"https:\/\/github.com\/driscollis\/python_logging_course\/blob\/main\/manuscript\/11_loguru.md#wrapping-up\" aria-label=\"Permalink: Wrapping Up\"><\/a><\/div>\n<p dir=\"auto\">The Loguru package makes logging much easier than Python&#8217;s\u00a0<code>logging<\/code>\u00a0library. It removes the boilerplate needed to create and format logs.<\/p>\n<p dir=\"auto\">In this chapter, you learned about the following:<\/p>\n<ul dir=\"auto\">\n<li>Installation<\/li>\n<li>Logging made simple<\/li>\n<li>Handlers and formatting<\/li>\n<li>Catching exceptions<\/li>\n<li>Terminal logging with color<\/li>\n<li>Easy log rotation<\/li>\n<\/ul>\n<p dir=\"auto\">Loguru can do much more than what is covered here, though. You can serialize your logs to JSON or contextualize your logger messages. Loguru also allows you to add lazy evaluation to your logs to prevent them from affecting performance in production. Loguru also makes adding custom log levels very easy. For full details about all the things Loguru can do, you should consult\u00a0<a href=\"https:\/\/loguru.readthedocs.io\/en\/stable\/overview.html\" rel=\"nofollow\">Loguru&#8217;s website<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/www.blog.pythonlibrary.org\/2024\/05\/15\/an-intro-to-logging-with-python-and-loguru\/\">An Intro to Logging with Python and Loguru<\/a> appeared first on <a href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Python\u2019s\u00a0logging\u00a0module isn\u2019t the only way to create logs. There are several third-party packages you can use, too. One of the most popular is\u00a0Loguru. Loguru intends to remove all the boilerplate you get with the Python logging API. You will find that Loguru greatly simplifies creating logs in Python. This chapter has the following sections: Installation [\u2026]<\/p>\n<p>The post <a href=\"https:\/\/www.blog.pythonlibrary.org\/2024\/05\/15\/an-intro-to-logging-with-python-and-loguru\/\">An Intro to Logging with Python and Loguru<\/a> appeared first on <a href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n<\/div>","protected":false},"author":2018,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"An Intro to Logging with Python and Loguru - ITTeacherITFreelance.hk","description":"Python\u2019s\u00a0logging\u00a0module isn\u2019t the only way to create logs. There are several third-party packages you can use, too. One of the most popular is\u00a0Loguru. Loguru in"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329577"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2018"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329577"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329577\/revisions"}],"predecessor-version":[{"id":329578,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329577\/revisions\/329578"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}