# Log Aggregation

## Summary


Log aggregation is the process of collecting, organizing, and analyzing log data from various sources across a system or network. Logs are records of events or messages generated by software, devices, or systems. Such logs are used to diagnose problems, monitor system performance, and identify security issues.

Log aggregation is especially important in complex systems and distributed environments, where logs are generated by a large number of components and can be difficult to access and analyze. By aggregating logs from different sources into a single centralized logging system, administrators can get a holistic view of system health and detect issues that may not be apparent when looking at individual logs.

Logs come in different formats, types and sources. Log aggregation tools are available to handle all aspects of log management including dashboards, alerts, and reports.

## Discussion

### Why do we need log aggregation?

In a monolithic architecture, all the components of the application are running on a single host. The logs are relatively easier to access and analyze. However, as the application grows, the logs can become unwieldy and difficult to manage. So log aggregation is still useful to centralize logs and make it easier to identify and resolve issues.

Log aggregation is even more critical in a microservices architecture because the services are distributed. Services generate logs on different hosts. This makes it harder to troubleshoot issues when they arise. Log aggregation leads to faster and more effective incident response.


### What's a typical log aggregation architecture for microservices?

In a typical architecture, each microservice stores log entries into a file or sends them to a log stream. A **log stream** can be thought of as a real-time, unstructured data feed of the activities and events occurring within a specific component or application.

On each host is an agent software called the **log collector**. It forwards the log to a **central log repository**. The centralized repository may be a database, a file system, or a cloud-based storage service. The log collector itself may be a separate process running on the host. Where the deployment is in a Kubernetes cluster, the log collector may be a separate container (aka sidecar container) running in the same pod as the main container.

Once logs are in the repository, there are **tools** to search, visualize and analyze them. ELK stack is one such tool. Logs can also be analyzed as they're being moved into the repository, what's called streaming analytics. This makes sense for critical applications that require real-time monitoring/metrics/alerts and immediate corrective action.


### What types of logs are involved in log aggregation?

Since it's hard to predict what sort of problems may crop up and where, a well-designed application must collect many types of logs. **Application logs** provide information about the behaviour of the application. **Infrastructure logs** provide information about the underlying system and its components. Both are important for troubleshooting issues. A good practice is to tag and structure the logs so that they can be easily searched and analyzed.

Application logs can be further sub-divided into security logs, audit logs, database logs, application server logs, middleware logs, and so on. Likewise, infrastructure logs can be further sub-divided into security logs, audit logs, operating system logs, network logs, and so on. Each log type must contain sufficient details relevant to its context. For example, security logs would include unauthorized access attempts. Database logs would include transactions. Middleware logs would contain information about message queues.

Metrics complement logs towards better monitoring and observability. **Metrics** are structured data points (CPU usage, memory usage, network traffic, response times, etc) collected at regular intervals. While logs provide a detailed record of individual events, metrics provide a high-level view of overall system performance.


### How are logs from different sources interleaved?

Logs can be interleaved at different stages of the log aggregation process, such as when they are collected by log collectors, stored in a database or file system, or analyzed by log search and analysis tools. The specific interleaving method used depends on the requirements of the system and the use case.

The most method is **time-based interleaving** that orders logs based on the time they were generated. This is useful for correlating events across different services or devices.

**Source-based interleaving** is more suited for analyzing logs from a specific component or service.

**Event-based interleaving** uses events or messages to organize logs. This is useful for identifying patterns and trends in the log data. A related method is **transaction-based interleaving**. This is useful for tracking the progress of a specific transaction or operation across different services.


### What log format should I adopt for log aggregation?

When choosing a log format for log aggregation, there are several factors to consider: level of detail, analysis tools, logging libraries in the selected programming language, etc. Some frameworks and libraries support many different formats. Examples include Log4j (Java logging library) and Fluentd (open-source data collector).

A popular format is **JSON (JavaScript Object Notation)** because of its flexibility and support in many programming languages. It's lightweight and text-based. It's easy to read and parse.

**Syslog** is a standard for logging messages and events. It's supported by many operating systems and network devices. It's commonly used in enterprise environments. Syslog messages are sent over UDP or TCP and can be easily collected by a Syslog server.

Apache's **Common Log Format (CLF)** is used for logging HTTP server requests. It's widely supported by web servers and web application frameworks. Like JSON, it's text-based and easy to parse and analyze.


### What are the best practices for log aggregation?

Define a clear logging strategy. Set out the goal of log analysis: identify bottlenecks, optimize performance, regulatory compliance, etc. Determine what log data you need to collect, how long you need to retain it, and what tools you will use for analysis and visualization. Regularly review the strategy since log aggregation is an ongoing process that requires regular maintenance and updates.

Collect logs in real time, rather than relying on batch processing or manual collection. If real-time analysis is not possible, at least regularly analyze logs. Use a standardized logging format, such as JSON or Syslog, to make it easier to parse and analyze logs.

Storing all logs in a central location is a pre-requisite. Implement security controls to protect log data, such as encrypting logs in transit and at rest, and restricting access to logs based on user roles.

Rotate log files regularly to prevent them from becoming too large. Monitor the system and set up alerts to warn of lost logs, bottlenecks or unusual activity.

Many of these best practices are also part of the CNCF guidelines for logging in cloud-native environments.


### What should developers and log analysts know about log aggregation?

Logs provide a wealth of information about application behaviour, including errors, warnings, performance metrics, trends and patterns. Developers should strive to make logs as useful as possible given this context. Too much logging can impact performance, especially if logging is synchronous. Developers must balance the need for information with the impact on performance.

Log data should be designed for machine consumption. This means logs should be standardized so that tools can process them effectively.

Logs are also data. Like in any data analytics workflow, log analytics has similar challenges: data volume, variety, quality, security, and accessibility. Authentication credentials and personally identifiable information (PII) should not be in the logs, or if present, should be perhaps encrypted. Logs should be clean of errors, duplicates, or other inconsistencies.

When troubleshooting problems with logging, check that the log source is actually generating and sending the logs. Check the log configuration. If logs are not reaching the central repository, check the log collector, network connections or firewall settings. Another technique is to increase the logging level to view detailed logs. To deal will log loss, implementing redundancy (multiple logging systems) may be necessary.


### What are some case studies of log aggregation?

Airbnb uses a combination of open-source tools, including Logstash, Kibana, and Elasticsearch, for log aggregation. The company collects log data from over 50,000 servers and analyzes over 200 GB of log data per day. Airbnb uses log aggregation to monitor application performance, detect security incidents, and troubleshoot issues.

Uber uses a custom-built log aggregation system called Marmaray to collect and analyze log data from its microservices architecture. Marmaray provides real-time log analysis and allows Uber to identify and troubleshoot issues quickly. Uber also uses Apache Kafka to collect log data from its applications and store it in a centralized location.

Netflix uses a combination of open-source tools, including Apache Kafka, Apache Cassandra, and Elasticsearch, for log aggregation. The company collects over 1 trillion events per day and also does real-time log analysis. In-house tools for log aggregation include the Spectator library for collecting application metrics, the Atlas service for storing and querying metric data, and the Mantis service for real-time stream processing.

## Milestones

1984

The **syslog** protocol is developed for UNIX systems. Syslog is a standard for logging messages, and it allows system administrators to collect and store logs from multiple sources. In 2000, a more advanced implementation of syslog called **rsyslog** is launch. It supports features such as filtering, log rotation, and remote logging.

1999

The concept of log aggregation gains popularity with the advent of **distributed systems and service-oriented architectures (SOA)** in the late 1990s. As the number of systems and services in these architectures increase, the need for a centralized approach to log management becomes more critical.

2000

In the early 2000s, the rise of **cloud computing** and the popularity of **containerization** leads to a new wave of log aggregation solutions. These solutions were designed to collect logs from virtual machines and containerized environments and store them in cloud-based log management platforms.

2006

The Apache Hadoop project is launched. Hadoop is a distributed computing platform that includes the Hadoop Distributed File System (HDFS) for storing and processing large datasets. Hadoop includes the **Hadoop Distributed Logging Service (HDFS)** for aggregating and analyzing log data.

2008

**Graylog** is launched. It's an open-source log management platform that includes features such as log aggregation, search, and visualization. Other tools follow: Logstash (2009), Elasticsearch (2010), Fluentd (2012) and Prometheus (2016).

2019

The Cloud Native Computing Foundation (CNCF) releases a set of **guidelines for logging in cloud-native environments**. The guidelines emphasize the importance of structured logging and recommend the use of tools such as Fluentd and Elasticsearch for log aggregation.