documents on wooden surface
Photo by AS Photography on Pexels.com

How to Save Operation Logs in Web System Development: A Beginner-Friendly Practical Guide

Introduction: The Importance and Purpose of Operation Logs

In web system development, “operation logs” that record user activity and system behavior are crucial for troubleshooting, security measures, and process improvement. By designing and managing operation logs appropriately, you can enhance both system reliability and operational efficiency.


Types of Operation Logs and What to Capture

Operation logs can be categorized as follows:

  • User Operation Logs: Records user actions (e.g., login, data entry, file downloads).
  • System Event Logs: Logs internal system events (e.g., errors, warnings, informational messages).
  • Access Logs: Tracks web server access information (e.g., IP address, access time, request content).
  • Database Operation Logs: Captures query history and transaction data for database access.
  • Environment Variables & Request Parameters: Helps identify root causes during issues by recording request headers, parameters, and environment values.

How to Capture Operation Logs

1. Logging at the Application Level

Embed logging logic within the application code. Most major frameworks offer built-in logging features.

Example: Logging in Java (Spring Boot)

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleController {
    private static final Logger logger = LoggerFactory.getLogger(SampleController.class);

    public void executeAction(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        String param = request.getParameter("id");
        logger.info("Action executed: User-Agent={}, Param id={}", userAgent, param);
    }
}

Example: Logging in Python (Django)

import logging

logger = logging.getLogger(__name__)

def sample_view(request):
    user_agent = request.META.get('HTTP_USER_AGENT', 'unknown')
    param_id = request.GET.get('id', 'not set')
    logger.info(f"Action executed: User-Agent={user_agent}, Param id={param_id}")

2. Logging at Middleware or Server Level

Utilize the built-in logging functions of web servers (e.g., Apache, Nginx) or application servers (e.g., Tomcat, Gunicorn) to capture access and error logs.

3. Logging on the Client Side

Use JavaScript to record browser-side events and send logs to the server.

window.onerror = function(message, source, lineno, colno, error) {
    fetch('/log', {
        method: 'POST',
        body: JSON.stringify({
            message: message,
            source: source,
            lineno: lineno,
            colno: colno,
            error: error.toString(),
            userAgent: navigator.userAgent
        }),
        headers: {
            'Content-Type': 'application/json'
        }
    });
};

Methods for Saving and Managing Operation Logs

1. Saving to Local Files

Logs are output to local files on the server. Suitable for small systems but may face scalability and maintenance issues.

2. Saving to a Database

Use a dedicated log table to store necessary data. Request parameters and environment variables are often stored in JSON format.

Example: Log Table Schema

Column Name Data Type Description
id SERIAL Unique identifier
user_id INTEGER ID of the user performing action
action VARCHAR(255) Type of action
request_data JSON Request parameters
env_variables JSON Environment variables (masked)
timestamp TIMESTAMP Date and time of the operation

※ Sensitive data (e.g., passwords, API keys) must be masked or excluded from logs.

3. Using Log Management Systems

Adopt tools like the ELK Stack or Fluentd to centrally manage and analyze large volumes of logs efficiently.


How to Utilize Operation Logs

  • Troubleshooting: Makes it easier to reproduce problems using recorded request and environment data.
  • Security Enhancement: Enables tracing of suspicious access or actions.
  • System Improvement: Analyzes user behavior to inform feature and UI improvements.
  • Audit Compliance: Provides traceability for operations to support legal or internal audits.

Best Practices for Log Management

  • Set Appropriate Log Levels: Avoid excessive logging by focusing on relevant information.
  • Mask Sensitive Information: Hide or exclude personal and authentication data.
  • Standardize Log Format: Use consistent and structured formats for easier analysis.
  • Implement Access Controls: Protect logs from unauthorized access with proper permissions.
  • Automate Monitoring and Archiving: Schedule periodic cleanup or archiving for stable operation.

Target Audience and Impact

This article is intended for engineers, operations staff, and security administrators involved in web system development. By understanding the importance of operation logs and learning how to properly capture, manage, and utilize them, you can improve system resilience and business efficiency. With beginner-friendly examples, it also serves as a useful primer for log design and implementation.


Conclusion

Capturing and storing operation logs is essential for ensuring the security and usability of web systems. Including request parameters and environment variables in your logs—and managing them appropriately—enables powerful troubleshooting and security measures. By improving the quality of your logs and establishing effective operational practices, you can build a more reliable system.

By greeden

Leave a Reply

Your email address will not be published. Required fields are marked *

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)