Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
IT Security ToolsAntiphishing v35456910988(21.09.2026 um 02:35 Uhr)
IT Security Toolsbrave-browser v1.98.12(21.09.2026 um 03:35 Uhr)
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
IT Security ToolsAntiphishing v35456910988(21.09.2026 um 02:35 Uhr)
IT Security Toolsbrave-browser v1.98.12(21.09.2026 um 03:35 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Mastering Python Logging: From Basics to Advanced Techniques

Reagiere als Erste:r — dein Feedback zählt!

Logging in Python is more than just debugging—it's about tracking, monitoring, and understanding your application’s behavior. Whether you're a beginner or an experienced developer, this guide covers all aspects of logging, from basic setups to advanced techniques.

Image description

Introduction
What is logging?
Logging is a mechanism to record and track events during the execution of a program, helping developers debug, monitor, and analyze their applications effectively.

Why is logging essential?
Unlike print, logging offers flexibility, scalability, and configurability, making it a robust choice for both small scripts and large applications.

What this blog covers
Setting up basic logging
Writing logs to files
Creating custom loggers
Formatting log outputs
Advanced techniques like log rotation and configurations
Best practices and common mistakes

What is Logging in Python?
Introduce the logging module.
Explain logging levels:
DEBUG: Detailed information for diagnosing issues.
INFO: Confirmation that the program is working as expected.
WARNING: Something unexpected happened, but the program can still run.
ERROR: A problem caused an operation to fail.
CRITICAL: A serious error that might stop the program.

Setting Up Basic Logging
Introduce logging.basicConfig.
Provide a simple example:

import logging

# Basic configuration
logging.basicConfig(level=logging.INFO)

# Logging messages
logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")

Output
By default, only messages at WARNING level or above are displayed on the console. The above example produces:

WARNING:root:Warning message
ERROR:root:Error message
CRITICAL:root:Critical message

Writing Logs to a File

logging.basicConfig(filename="app.log", 
                    level=logging.DEBUG, 
                    format="%(asctime)s - %(levelname)s - %(message)s")

logging.info("This will be written to a file.")

Explain common parameters in basicConfig:
filename: Specifies the log file.
filemode: 'w' to overwrite or 'a' to append.
format: Customizes log message structure.

Creating Custom Loggers
Why use custom loggers? For modular and more controlled logging.
Example:

import logging

# Create a custom logger
logger = logging.getLogger("my_logger")
logger.setLevel(logging.DEBUG)

# Create handlers
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler("custom.log")

# Set levels for handlers
console_handler.setLevel(logging.INFO)
file_handler.setLevel(logging.ERROR)

# Create formatters and add them to handlers
formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)

# Add handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)

# Log messages
logger.info("This is an info message.")
logger.error("This is an error message.")

** Formatting Logs**
Explain log record attributes:
%(asctime)s: Timestamp.
%(levelname)s: Level of the log message.
%(message)s: The actual log message.
%(name)s: Logger's name.
Advanced formatting:

logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                    datefmt="%Y-%m-%d %H:%M:%S",
                    level=logging.DEBUG)

Log Rotation
Introduce RotatingFileHandler for managing log file size.
Example:

from logging.handlers import RotatingFileHandler

# Create a logger
logger = logging.getLogger("rotating_logger")
logger.setLevel(logging.DEBUG)

# Create a rotating file handler
handler = RotatingFileHandler("app.log", maxBytes=2000, backupCount=3)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)

logger.addHandler(handler)

# Log messages
for i in range(100):
    logger.info(f"Message {i}")

Using logging.config for Complex Configurations
Show how to use a configuration dictionary:

import logging
import logging.config

log_config = {
    "version": 1,
    "formatters": {
        "default": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "default"
        },
        "file": {
            "class": "logging.FileHandler",
            "filename": "config.log",
            "formatter": "default"
        }
    },
    "loggers": {
        "": {  # Root logger
            "handlers": ["console", "file"],
            "level": "INFO"
        }
    }
}

logging.config.dictConfig(log_config)
logger = logging.getLogger("custom_logger")
logger.info("This is a log from the configured logger.")

Best Practices for Logging
Use meaningful log messages.
Avoid sensitive data in logs.
Use DEBUG level in development and higher levels in production.
Rotate log files to prevent storage issues.
Use unique logger names for different modules.

Common Mistakes
Overusing DEBUG in production.
Forgetting to close file handlers.
Not using a separate log file for errors.

Advanced Topics
Asynchronous Logging
For high-performance applications, use QueueHandler to offload logging tasks asynchronously.

Structured Logging
Log messages as JSON to make them machine-readable, especially for systems like ELK Stack.

Third-Party Libraries
Explore tools like loguru for simpler and more powerful logging.

Conclusion
Logging is not just about debugging—it's about understanding your application. By mastering Python's logging module, you can ensure your projects are robust, maintainable, and easy to debug.

Have questions or suggestions? Share your thoughts in the comments below!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Mastering Python Logging: From Basics to Advanced Techniques

Thematisch verwandte Begriffe: Mastering, Python, Logging, From · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93977 | A vulnerability was determined in code-projects Assessment Management 1.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick