Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      7 MagSafe accessories that I recommend every iPhone user should have

      June 1, 2025

      I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

      June 1, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

      June 1, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025
      Recent

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025

      Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 22/2025

      June 1, 2025

      Rilasciata PorteuX 2.1: Novità e Approfondimenti sulla Distribuzione GNU/Linux Portatile Basata su Slackware

      June 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»What Are Logs in Programming?

    What Are Logs in Programming?

    February 12, 2025

    Have you ever run a program, and it crashed? No error messages, no hints, just silence. How do you figure out what went wrong? That’s where logging saves the day.

    Logs keep track of what’s happening inside your code so that when things go wrong, you don’t have to guess. They’re similar to print or console.log, but more powerful.

    In this tutorial, I will use Python to create and walk you through some logging code examples.

    Before we talk about logs, let’s understand the different error types you might use or encounter.

    Types of Errors

    When you’re building a production-level application, you need to display errors based on their severity. There are several error types, and the most important ones are:

    • DEBUG: Detailed information, typically useful for diagnosing problems.

    • INFO: General information about the program’s progress.

    • WARNING: Something unexpected happened, but it’s not critical.

    • ERROR: An error occurred, but the program can still run.

    • CRITICAL: A very serious error that may stop the program from running.

    What is Logging?

    Now, let’s get straight to the point and understand what logging is.

    In simple terms, logs or logging is the act of recording information about everything your program does. The recorded information could be anything, from basic details like which functions were called to more detailed ones like tracking errors or performance issues.

    Why Do We Need Logging?

    You might be thinking, “If logs are printing errors, info, and so on, I can just use print statements. Why do I need logging?” Well, print works, but logging gives you more control:

    ↳ It can store messages in a file.
    ↳ It has different levels (info, warning, error, and so on).
    ↳ You can filter messages based on importance.
    ↳ It helps in debugging without cluttering your code.

    These are things print statements can’t do effectively.

    How to Add Logs in Python

    In Python, the logging module is built specifically for logging purposes.

    Let’s set up some logs to see how they work.

    Step 1: Import the Logging Module

    To start using logging, we need to import the module:

    import logging
    

    Step 2: Log Messages

    Now, you can start logging messages in your program. You can use different log levels based on the importance of the message. As a reminder, those levels are (from least to most urgent):

    • DEBUG

    • INFO

    • WARNING

    • ERROR

    • CRITICAL

    Let’s log a simple message at each level:

    logging.debug("This is a debug message")
    logging.info("This is an info message")
    logging.warning("This is a warning message")
    logging.error("This is an error message")
    logging.critical("This is a critical message")
    

    When you run this, you’ll see a message printed to the console, similar to this:

    Terminal showing Python log messages.

    You might wonder why you don’t see the DEBUG and INFO messages. The default logging level prevents this.

    By default, the logging level is set to WARNING. This means that only messages with a severity of WARNING or higher will be displayed (that is, WARNING, ERROR, and CRITICAL).

    Step 3: Set Up the Basic Configuration

    To see the debug and info messages, we need to set the logging level DEBUG before running the code.

    This means we need to configure the logs. So to do this, use the method basicConfig below:

    logging.basicConfig(level=logging.DEBUG)
    

    This basic configuration allows you to log messages at the DEBUG level or higher. You can change the level depending on the type of logs you want.

    Now, all logs are printing:

    log messages: debug, info, warning, error, critical.

    Step 4: Log to a File

    Now, let’s save these logs in a file so we can keep track of errors, as well as when they occurred. To do this, update the configuration:

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

    Here:

    • asctime – The time when the event occurred.

    • levelname – The type of the log (for example, DEBUG, INFO).

    • message – The message we display.

    Now, when you run the program, the log file will generate and save your logs, showing the exact timing, error type, and message. Like this:

    Log file with debug, info, warning, error, critical messages

    How to Use Loggers for More Control

    If you’re working on a large project, you might want a utility logger that you can use anywhere in the code. Let’s create this custom logger.

    First, we’ll update the basicConfig to add the filename, line number, and ensure it writes everything, even special characters:

    logging.basicConfig(
            filename=log_file,
            level=logging.DEBUG,
            format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s', 
            filemode='w',
            encoding='utf-8' 
        )
    

    Explanation:

    • encoding='utf-8' — Ensures special characters are logged.

    • %(filename)s:%(lineno)d — Logs the filename and line number where the log was generated.

    Now, let’s set up a custom console logger:

      console_handler = logging.StreamHandler()
      console_handler.setLevel(logging.DEBUG)
      console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s')  # Added line number
      console_handler.setFormatter(console_formatter)
    
    
       logging.getLogger().addHandler(console_handler)
    

    This setup does the following:

    • console_handler: Sends log messages to the console (stdout).

    • console_formatter: Formats the log message with time, level, filename, line number, and the message.

    • logging.getLogger().addHandler(console_handler): Adds the custom handler to the root logger, so the log messages are printed to the console.

    Full Example Code

    import logging
    import os
    from datetime import datetime
    
    def setup_daily_logger():
        base_dir = os.path.dirname(os.path.abspath(__file__))
        log_dir = os.path.join(base_dir, 'logs')  
        os.makedirs(log_dir, exist_ok=True)
    
    
        current_time = datetime.now().strftime("%m_%d_%y_%I_%M_%p")
        log_file = os.path.join(log_dir, f"{current_time}.log")
    
    
        logging.basicConfig(
            filename=log_file,
            level=logging.DEBUG,
            format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s', 
            filemode='w',
            encoding='utf-8' 
        )
    
    
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
        console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s')  # Added line number
        console_handler.setFormatter(console_formatter)
    
    
        logging.getLogger().addHandler(console_handler)
    
    
        return logging.getLogger(__name__)
    

    What Happens Now?

    Now, whenever you run the program, a new log file will be created in the logs folder. Each time the program is executed, a new log file with a unique timestamp will be generated.

    Like this:

    custom log used in app.py

    These logs will give you a clear picture of your program’s behavior and help with debugging.

    I hope this article helped you get a clearer picture of logs and their importance in programming.

    Practical Real-World Examples

    Now that you understand what logs are and how to set them up in Python, let’s look at real-world use cases.

    1. Bot: Scraping Korea’s Largest Property Website

    Here’s an example of a bot designed to scrape Korea’s biggest property website.

    • The logs show every step the bot takes, making it easier to track progress.

    • If an error occurs at any step, it gets recorded in the log file.

    • Even if the bot crashes, I can check the logs to pinpoint where things went wrong.

    Log file with INFO messages showing city and town extraction details.

    Log file with INFO messages showing city and town extraction details.

    One of the methods in this bot’s class uses logging to track whether the bot correctly selects the province.

    select_province function that utilizes logging

    Here:

    • If an error or warning occurs, it’s saved in the log file.

    • Later, you can review the logs and find out exactly what happened

    2. Bot: Scraping Facebook Groups

    Now, let’s see how logging helps in a Facebook group scraper.

    Error Tracking
    • At one point, the bot failed due to an error.

    • Since we had logging in place, the error was saved in the log file.

    • This allows you to quickly find out what went wrong.

    Error log file

    Here, you see the exact filename and line number where the error occurs.

    Logs file shows success logs

    Once we identified and fixed the issue, the bot started working again.

    It captures every detail in the log, saving hours of debugging by pinpointing where errors occur.

    Debugging Made Easy
    • The logs recorded every detail of the bot’s execution.

    • This can save you hours of debugging because you’ll know exactly where the error occurred.

    Conclusion

    Logging is one of those things no one thinks about until something breaks. But when it does, logs become your best friend.

    Remember:

    • Logging isn’t just for error tracking—it helps you monitor your program’s flow.

    • Instead of guessing what went wrong, check the logs. The answer is usually right there.

    Make sure to add logging to your code. You’ll thank yourself later!

    Stay Connected – @syedamahamfahim 🐬

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleJavaScript Tutorial – How to Set Up a Front End Development Project
    Next Article The Debian Project mourns the loss of Steve Langasek (vorlon)

    Related Posts

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 1, 2025
    Artificial Intelligence

    LWiAI Podcast #201 – GPT 4.5, Sonnet 3.7, Grok 3, Phi 4

    June 1, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    Schleswig-Holstein: 30.000 PC Passano a LibreOffice e Linux

    Linux

    CVE-2025-47887 – Jenkins Cadence vManager Plugin Unauthenticated Remote Code Execution

    Common Vulnerabilities and Exposures (CVEs)

    Salesforce AI Research Introduce xGen-MM (BLIP-3): A Scalable AI Framework for Advancing Large Multimodal Models with Enhanced Training and Performance Capabilities

    Development

    GNOME Maps – find places around the world

    Linux

    Highlights

    The best cheap Android phone I’ve used this year can be had for free

    June 19, 2024

    T-Mobile’s $250 Revvl 7 Pro covers the basics with a large display, long-lasting battery, and…

    Adobe Announces Monthly Subscription for the Undo Button

    April 1, 2025

    Evaluating potential cybersecurity threats of advanced AI

    May 27, 2025

    NVIDIA AI Releases Introduce UltraLong-8B: A Series of Ultra-Long Context Language Models Designed to Process Extensive Sequences of Text (up to 1M, 2M, and 4M tokens)

    April 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.