fastlogging


Namefastlogging JSON
Version 1.2.0 PyPI version JSON
download
home_pagehttps://github.com/brmmm3/fastlogging
SummaryA fast logger
upload_time2024-07-26 07:08:52
maintainerNone
docs_urlNone
authorMartin Bammer
requires_pythonNone
licenseMIT License Copyright (c) 2018 Martin Bammer 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.
keywords fast logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            An efficient and feature-rich logging module
============================================

.. role:: Python(code)
   :language: Python

The ``fastlogging`` module is a faster replacement of the standard logging module with a mostly compatible API.

It comes with the following features:

 - (colored, if colorama is installed) logging to console
 - logging to file (maximum file size with rotating/history feature can be configured)
 - old log files can be compressed (the compression algorithm can be configured)
 - count same successive messages within a 30s time frame and log only once the message with the counted value.
 - log domains
 - log to different files
 - writing to log files is done in (per file) background threads, if configured
 - configure callback function for custom detection of same successive log messages
 - configure callback function for custom message formatter
 - configure callback function for custom log writer

The API is described `here <doc/API.rst>`_.

Installation
------------

Simply run

.. code-block:: Python

    python setup.py install --user

or create a wheel and install it.

.. code-block:: Python

    python setup.py bdist_wheel

An optimized version of ``fastlogging`` will be installed if package **cython** is installed.
If you need a pure python version of the ``fastlogging`` module then add option **nocython**.

Usage
-----

.. code-block:: Python

    from fastlogging import LogInit

    logger = LogInit(pathName="/tmp/example1.log", console=True, colors=True)
    logger.debug("This is a debug message.")
    logger.info("This is an info message.")
    logger.warning("This is a warning message.")
    logger.rotate()
    logger.fatal("This is a fatal message.")
    logger.shutdown()

The example above writes all messages to a file and to the console. On the console the messages are printed
with colors. With the rotate call the log file is renamed to `example1.log.1` and a new log file is created.

The second example creates a server socket on localhost and writes all messages to a log file for 15 seconds.

.. code-block:: Python

    import os
    import time

    from fastlogging import LogInit

    addr = "127.0.0.1"
    port = 12345
    pathName = "C:/temp/server.log" if os.name == 'nt' else "/tmp/server.log"
    logger = LogInit(pathName=pathName, server=(addr, port))
    logger.info("Logging started.")
    logger.debug("This is a debug message.")
    logger.info("This is an info message.")
    logger.warning("This is a warning message.")
    time.sleep(15)
    logger.info("Shutdown logging.")
    logger.shutdown()


And now the third example connects to the log server and sends 300000 messages.

.. code-block:: Python

    import os
    import time

    from fastlogging import LogInit

    addr = "127.0.0.1"
    port = 12345
    logger = LogInit(connect=(addr, port, "HELLO%d" % os.getpid()))
    for i in range(100000):
        logger.debug("This is a DBG message %d." % i)
        logger.info("This is an INF message %d." % i)
        logger.warning("This is a WRN message %d." % i)
    time.sleep(10.0)
    logger.shutdown()

The messages are sent in blocks to improve speed.

Optimizing for speed
--------------------

As you can see in the charts below fastlogging is much faster than the default logging module which comes
with Python (red bar).

You also can see that using threads can be slower than writing logs directly to the
file, because of additional overhead. So threads should only be used if you've got a slow disk and lot's of
messages to log.

There are 3 more bars which show even better performance. To understand the optimizations a deeper look into
a logging line has to be done.

Let's analyze what is going on when the following code line is executed:

.. code-block:: Python

    logger.debug("This is a debug message.")

The Python interpreter first creates a tuple for the positioned arguments and a dictionary for the named
arguments. Then it calls method ``info``. In method ``info`` the log level is checked against the severity.
Only if the severity is high enough the message will be logged.

Now what if we set a **if** before the above line?

.. code-block:: Python

    if logger.level <= DEBUG:
        logger.debug("This is a debug message.")

Running benchmarks will show us that the code runs faster now if the log level is higher than DEBUG.
Normally we need debug messages only in case of development or bugfixing. So it makes sense to optimize
such lines. But doing this manually is awkward and bloats the code.

To simplify this task the ``fastlogging`` module comes with an `AST optimizer <doc/Optimize.rst>`_ which does the work for you.


Benchmarks
----------

The following benchmarks were measured on Ubuntu 18.10 with a Ryzen 7 CPU and an SSD.

You can see that ``fastlogging`` is **~5x** faster when rotating is disabled and **>13x** faster in case of log rotating.



.. figure:: doc/benchmarks/log.png

   Benchmark results with a single log files

.. figure:: doc/benchmarks/rotate.png

   Benchmark results with rotating log files

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/brmmm3/fastlogging",
    "name": "fastlogging",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "fast, logging",
    "author": "Martin Bammer",
    "author_email": "Martin Bammer <mrbm74@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e3/3a/a170d0b3e0c6a4f24646bcefc341fe2e7cfa6e2b1843f32b42414073d359/fastlogging-1.2.0.tar.gz",
    "platform": null,
    "description": "An efficient and feature-rich logging module\n============================================\n\n.. role:: Python(code)\n   :language: Python\n\nThe ``fastlogging`` module is a faster replacement of the standard logging module with a mostly compatible API.\n\nIt comes with the following features:\n\n - (colored, if colorama is installed) logging to console\n - logging to file (maximum file size with rotating/history feature can be configured)\n - old log files can be compressed (the compression algorithm can be configured)\n - count same successive messages within a 30s time frame and log only once the message with the counted value.\n - log domains\n - log to different files\n - writing to log files is done in (per file) background threads, if configured\n - configure callback function for custom detection of same successive log messages\n - configure callback function for custom message formatter\n - configure callback function for custom log writer\n\nThe API is described `here <doc/API.rst>`_.\n\nInstallation\n------------\n\nSimply run\n\n.. code-block:: Python\n\n    python setup.py install --user\n\nor create a wheel and install it.\n\n.. code-block:: Python\n\n    python setup.py bdist_wheel\n\nAn optimized version of ``fastlogging`` will be installed if package **cython** is installed.\nIf you need a pure python version of the ``fastlogging`` module then add option **nocython**.\n\nUsage\n-----\n\n.. code-block:: Python\n\n    from fastlogging import LogInit\n\n    logger = LogInit(pathName=\"/tmp/example1.log\", console=True, colors=True)\n    logger.debug(\"This is a debug message.\")\n    logger.info(\"This is an info message.\")\n    logger.warning(\"This is a warning message.\")\n    logger.rotate()\n    logger.fatal(\"This is a fatal message.\")\n    logger.shutdown()\n\nThe example above writes all messages to a file and to the console. On the console the messages are printed\nwith colors. With the rotate call the log file is renamed to `example1.log.1` and a new log file is created.\n\nThe second example creates a server socket on localhost and writes all messages to a log file for 15 seconds.\n\n.. code-block:: Python\n\n    import os\n    import time\n\n    from fastlogging import LogInit\n\n    addr = \"127.0.0.1\"\n    port = 12345\n    pathName = \"C:/temp/server.log\" if os.name == 'nt' else \"/tmp/server.log\"\n    logger = LogInit(pathName=pathName, server=(addr, port))\n    logger.info(\"Logging started.\")\n    logger.debug(\"This is a debug message.\")\n    logger.info(\"This is an info message.\")\n    logger.warning(\"This is a warning message.\")\n    time.sleep(15)\n    logger.info(\"Shutdown logging.\")\n    logger.shutdown()\n\n\nAnd now the third example connects to the log server and sends 300000 messages.\n\n.. code-block:: Python\n\n    import os\n    import time\n\n    from fastlogging import LogInit\n\n    addr = \"127.0.0.1\"\n    port = 12345\n    logger = LogInit(connect=(addr, port, \"HELLO%d\" % os.getpid()))\n    for i in range(100000):\n        logger.debug(\"This is a DBG message %d.\" % i)\n        logger.info(\"This is an INF message %d.\" % i)\n        logger.warning(\"This is a WRN message %d.\" % i)\n    time.sleep(10.0)\n    logger.shutdown()\n\nThe messages are sent in blocks to improve speed.\n\nOptimizing for speed\n--------------------\n\nAs you can see in the charts below fastlogging is much faster than the default logging module which comes\nwith Python (red bar).\n\nYou also can see that using threads can be slower than writing logs directly to the\nfile, because of additional overhead. So threads should only be used if you've got a slow disk and lot's of\nmessages to log.\n\nThere are 3 more bars which show even better performance. To understand the optimizations a deeper look into\na logging line has to be done.\n\nLet's analyze what is going on when the following code line is executed:\n\n.. code-block:: Python\n\n    logger.debug(\"This is a debug message.\")\n\nThe Python interpreter first creates a tuple for the positioned arguments and a dictionary for the named\narguments. Then it calls method ``info``. In method ``info`` the log level is checked against the severity.\nOnly if the severity is high enough the message will be logged.\n\nNow what if we set a **if** before the above line?\n\n.. code-block:: Python\n\n    if logger.level <= DEBUG:\n        logger.debug(\"This is a debug message.\")\n\nRunning benchmarks will show us that the code runs faster now if the log level is higher than DEBUG.\nNormally we need debug messages only in case of development or bugfixing. So it makes sense to optimize\nsuch lines. But doing this manually is awkward and bloats the code.\n\nTo simplify this task the ``fastlogging`` module comes with an `AST optimizer <doc/Optimize.rst>`_ which does the work for you.\n\n\nBenchmarks\n----------\n\nThe following benchmarks were measured on Ubuntu 18.10 with a Ryzen 7 CPU and an SSD.\n\nYou can see that ``fastlogging`` is **~5x** faster when rotating is disabled and **>13x** faster in case of log rotating.\n\n\n\n.. figure:: doc/benchmarks/log.png\n\n   Benchmark results with a single log files\n\n.. figure:: doc/benchmarks/rotate.png\n\n   Benchmark results with rotating log files\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2018 Martin Bammer  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. ",
    "summary": "A fast logger",
    "version": "1.2.0",
    "project_urls": {
        "Download": "https://github.com/brmmm3/fastlogging/releases/download/1.2.0/fastlogging-1.2.0.tar.gz",
        "Homepage": "https://github.com/brmmm3/fastlogging"
    },
    "split_keywords": [
        "fast",
        " logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e33aa170d0b3e0c6a4f24646bcefc341fe2e7cfa6e2b1843f32b42414073d359",
                "md5": "a22082623802e4aa9651367158d5e578",
                "sha256": "35541c257bf5b66425af7bcf6bf2be3f0aa5ba55d92344c1d1eddf4991d35d90"
            },
            "downloads": -1,
            "filename": "fastlogging-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a22082623802e4aa9651367158d5e578",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19363,
            "upload_time": "2024-07-26T07:08:52",
            "upload_time_iso_8601": "2024-07-26T07:08:52.570170Z",
            "url": "https://files.pythonhosted.org/packages/e3/3a/a170d0b3e0c6a4f24646bcefc341fe2e7cfa6e2b1843f32b42414073d359/fastlogging-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-26 07:08:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "brmmm3",
    "github_project": "fastlogging",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "fastlogging"
}
        
Elapsed time: 0.53817s