{"id":4620,"library":"logzero","title":"logzero","description":"logzero is a Python library (version 1.7.0) that provides robust and effective logging for both Python 2 and 3. It simplifies logging to the console with pretty, level-specific colored output and supports logging to rotating files. The library also includes built-in JSON logging capabilities, aiming for a low-boilerplate approach to standard Python logging. Its release cadence is generally stable with occasional updates.","status":"active","version":"1.7.0","language":"en","source_language":"en","source_url":"https://github.com/metachris/logzero","tags":["logging","console logging","file logging","pretty logging","json logging","developer tool"],"install":[{"cmd":"pip install logzero","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Used for JSON logging functionality (logzero.json() or setup_logger(json=True)).","package":"python-json-logger","optional":true},{"reason":"Enables colored console output on Windows systems.","package":"colorama","optional":true}],"imports":[{"symbol":"logger","correct":"from logzero import logger"},{"note":"Many configuration functions like `logfile`, `loglevel`, `json` are typically called as `logzero.function_name()` after `import logzero`.","wrong":"from logzero import logfile # While possible, direct import of module-level functions is less common than importing 'logger' and accessing functions via 'logzero.' prefix.","symbol":"logzero module functions","correct":"import logzero\nlogzero.logfile('/path/to/file.log')"}],"quickstart":{"code":"from logzero import logger, logfile\nimport os\n\n# Configure a logfile that rotates after 1MB, keeping 3 backups\nlog_file_path = os.path.join(os.getcwd(), \"app.log\")\nlogfile(log_file_path, maxBytes=1_000_000, backupCount=3)\n\n# Set the global minimum log level to INFO\nlogger.setLevel(os.environ.get('LOG_LEVEL', 'INFO'))\n\nlogger.debug(\"This is a debug message - won't show with INFO level.\")\nlogger.info(\"Application started successfully.\")\nlogger.warning(\"Disk space is getting low.\")\n\ntry:\n    1 / 0\nexcept ZeroDivisionError as e:\n    logger.exception(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates basic console and rotating file logging, setting a global log level, and handling exceptions with `logger.exception()`."},"warnings":[{"fix":"Be aware that log output goes to stderr. If you need it in stdout, you might need to redirect stderr to stdout at the shell level, or configure a custom handler to explicitly output to `sys.stdout`.","message":"logzero outputs all log messages to `stderr` by default, not `stdout`. Users expecting `print()`-like behavior to `stdout` might find their logs redirected, requiring explicit redirection if they want `stdout` for other application output.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always set `maxBytes` and `backupCount` to non-zero values when calling `logzero.logfile()` if log file rotation is desired (e.g., `logzero.logfile('/path/to/app.log', maxBytes=1_000_000, backupCount=3)`).","message":"When using `logzero.logfile()`, the default parameters `maxBytes=0` and `backupCount=0` disable log file rotation. This means log files will grow indefinitely unless these parameters are explicitly configured to enable rotation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For specific logging configurations in different parts of your application, use `logzero.setup_logger(name=...)` to create isolated logger instances instead of relying solely on the global `logzero.logger` and its module-level configuration functions.","message":"Functions like `logzero.loglevel()`, `logzero.logfile()`, and `logzero.formatter()` globally reconfigure the *default* `logzero.logger` instance and all its internal handlers. This can unintentionally affect logging behavior across different modules or parts of a larger application if not managed carefully.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When using custom formatters for file logging, ensure that color codes are either removed from the format string or handled by a custom `Formatter` class that strips them for file output, or configure separate formatters for stream and file handlers.","message":"If a custom formatter (set via `logzero.formatter()`) includes ANSI color codes and is applied to file handlers without stripping them, log files may contain these escape sequences, making them harder to read in plain text editors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}