Skip to main content

A simple Python library for logging with console and file output

Project description

nysimplelog

Python 3.10+

nysimplelog is a simple Python library for logging. It outputs logs to both the console and a file without relying on the standard Python logging.basicConfig. Logging can be configured between different modules.

1. Requirements

  • Python 3.10 or higher (for full type hint support)
  • tzdata (required on Windows for timezone support)

2. Installation

Install with pip:

pip install nysimplelog

3. Usage

3.1 Import Packages

For the main program:

from logging import getLogger, WARNING, INFO, DEBUG
from nysimplelog import initialize_simple_logger

For modules:

from logging import getLogger

3.2 Create Logger

Return a logger with the specified name. You can use the module name by using __name__ in the logger name.

logger = getLogger(__name__)
  or
logger = getLogger("example")

3.3 Initialize Simple Logger

Initialize the logger with custom settings. This function returns a configured logger.

logger = initialize_simple_logger(
    name="example",
    log_dir="logs",
    fmt="%(asctime)s %(levelname)s %(name)s - %(message)s", # Custom format
    datefmt="%Y-%m-%dT%H:%M:%S", # Custom date format
    tz="Asia/Tokyo"
    level="DEBUG",
    file_handler_level=WARNING, # File logs warnings and above
    maxBytes=1024 * 1024, # 1MB (default is 10MB)
    backupCount=5, # 5 log files (default is 2)
)

Options

Parameter Description
name The name of the logger.
log_dir Directory for log files. Default: "logs".
filename Log file name. Default: "{log_dir}/{name}.log".
fmt Log message format. Default: "%(asctime)s %(levelname)s %(name)s - %(message)s".
datefmt Date format. Default: ISO-8601 (e.g., "2023-05-27T07:20:32.798+09:00").
tz Timezone for log timestamps (e.g., "UTC", "Asia/Tokyo"). Default: None (uses local time).
formatter Custom formatter. Default: None (uses ISO8601_Formatter).
level Logger level (int or str). Default: logging.WARNING.
stream_handler_level Stream handler level. Default: if None, the parameter level is used.
file_handler_level File handler level. Default: if None, the parameter level is used.
maxBytes Max file size before rotation. Default: 10MB.
backupCount Number of backup log files. Default: 2.

Note: On Windows, run pip install tzdata to use the tz parameter with specific timezones.

3.4 Logging Messages

See Python's logging docs for details. Use standard logging methods:

logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")

4. Example

4.1 Directory Structure

examples/example_usage.py
examples/mypackage/__init__.py

For a complete sample project, see the examples folder in the GitHub repository.

4.2 Module Example

# examples/mypackage/__init__.py
from logging import getLogger

logger = getLogger(__name__)

def hello():
    logger.debug("Hello, world!")

4.3 Main Program

# examples/example_usage.py
from logging import getLogger, WARNING, INFO, DEBUG
from nysimplelog import initialize_simple_logger

import mypackage

# Main logger with different levels for stream and file
logger = initialize_simple_logger(
    name="main",
    level=DEBUG,
    stream_handler_level=INFO,  # Console logs info and above
    file_handler_level=WARNING  # File logs warnings and above
)

# Module-specific logger
initialize_simple_logger(name=mypackage.__name__, level=DEBUG)

logger.debug("This is a debug message")  # Not displayed
logger.info("This is an info message")   # Only in file (if level allows)
logger.warning("This is a warning")      # Console and file
mypackage.hello()                        # Depends on mypackage logger

4.4 Output

Console:

2025-03-02T18:01:28.413+09:00 INFO main - This is an info message
2025-03-02T18:01:28.414+09:00 WARNING main - This is a warning
2025-03-02T18:01:28.414+09:00 DEBUG mypackage - Hello, world!

File logs/main.log:

2025-03-02T18:01:28.414+09:00 WARNING main - This is a warning

Files are stored in logs/ with rotation (e.g. main.log, main.log.1 etc.).

5. Features

  • Logging can be configured for different modules.
  • Supports standard output and file output.
  • Supports log file rotation.
  • Uses ISO-8601 as default date format.

6. Development

6.1 Virtual Environment Setup

# Create virtual environment
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate

6.2 Dependencies Installation

pip install -r requirements-dev.txt

6.3 Test Execution

pytest

6.4 Package Build

python -m build

7. Author

Developed by Naoyuki Yoshinori

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

nysimplelog-1.0.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nysimplelog-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file nysimplelog-1.0.0.tar.gz.

File metadata

  • Download URL: nysimplelog-1.0.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for nysimplelog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 85353c4ade2d45f382f762a3d0c0bf2f3d1edd8e402f6a57e52c2b59a8a43c1b
MD5 cd88e8c877a955d7334e564d4e07b2ca
BLAKE2b-256 0be44bc9f3825f787e90fc552526e85e4afda89e00d430eb141b352006377dc0

See more details on using hashes here.

File details

Details for the file nysimplelog-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nysimplelog-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for nysimplelog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a63142c98b0825d634613117573e252d914321ee7159b0823cfc0ec4f4cf538e
MD5 cb6fd25df1e35c93c5f11940d4db707d
BLAKE2b-256 f5eb6e93df32eb750e8265bb2750f0d72e700876746724add2e6f315d43362b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page