Skip to main content

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.INFO.
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

Metadata

Release files for nysimplelog 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nysimplelog 1.0.1
File Size Uploaded Provenance
nysimplelog-1.0.1.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for nysimplelog 1.0.1
File Interpreter ABI Platform
nysimplelog-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / nysimplelog-1.0.1.tar.gz

Download URL nysimplelog-1.0.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d248bdb33da45cd3b0b454843155abd833ef6ce8baa317612fb5993e85eac14f
BLAKE2b-256 checksum
How to use checksums
7d164b064e6617bd79657eab7be8722be5e0f414d7321baf0b13b5be1f3822ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / nysimplelog-1.0.1-py3-none-any.whl

Download URL nysimplelog-1.0.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a5e50ee44cc8e9e08b472d27bd851047bbfb6179e17b2f1a8e80fce4ec7a8bb
BLAKE2b-256 checksum
How to use checksums
c7d1e2abfc1d0b7e9034cd3da8879ba537b6711f6bc626c8e948f46aae64758f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Security for nysimplelog 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page