The log collection scheme I have been using before is ELK, which often takes up several gigabytes of memory, and some poorly configured servers can’t stand it! I recently discovered a lightweight log collection solution: Loki+ PromTail +Grafana (LPG for short), with a few hundred MB of memory and a nice interface. I recommend it to you!

Mall SpringBoot practical electricity project (50 k + star) address: https://github.com/macrozheng/mall

Introduction to the

The LPG log collection scheme takes up very little memory, is economical and efficient! Instead of indexing the logs as the ELK logging system does, it sets a set of tags for each log stream. Here are its core components:

  • PromTail: A log collector, a bit like FileBeat, that collects logs from log files and pushes the collected data into Loki.
  • Loki: Aggregate and store log data that can be used as Grafana’s data source to provide Grafana with visual data.
  • Grafana: Retrieves log information from Loki for visual display.

The installation

To implement this log collection solution, you need to install services like Loki, PromTail, and Grafana and use them directly
docker-composeIt is very convenient to install.

  • The use ofdocker-compose.ymlThe script is as follows. Use it directlydocker-composeCommand run can;
Loki: image: grafana/ Loki container_name: LPG-Loki volumes: container_name: LPG-Loki volumes: - /mydata/ Loki /:/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ -config =/etc/loki/ PromTail container_name: LPG - PromTail volumes: Container_Name - PromTail volumes: # will need to collect the log directory mounted to promtail containers - / mydata/app/mall - tiny - Loki/logs /, / var/log / - / mydata/promtail: / etc/promtail / # Modify the default configuration file path promtail command: - config. The file = / etc/promtail/promtail yml # log visualization grafana: image: grafana/grafana container_name: lpg-grafana ports: - 3000:3000
  • Since we have the Loki and PromTail profiles mounted on the host, we need to have both profiles ready before running them;
  • Loki configuration file/mydata/loki/loki.ymlThe following is the default configuration (you can run Loki’s Docker container without mounting the configuration file and then copy it from the container);
Auth_enabled: false server: http_listen_port: 3100 ingester: lifecycler: address: 127.0.0.1 ring: kvstore: store: inmemory replication_factor: 1 final_sleep: 0s chunk_idle_period: 1h # Any chunk not receiving new logs in this time will be flushed max_chunk_age: 1h # All chunks will be flushed when they hit this age, default is 1h chunk_target_size: Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first chunk_retain_period: 30s # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m) max_transfer_retries: 0 # Chunk transfers disabled schema_config: configs: - from: 2020-10-24 store: boltdb-shipper object_store: filesystem schema: v11 index: prefix: index_ period: 24h storage_config: boltdb_shipper: active_index_directory: /loki/boltdb-shipper-active cache_location: /loki/boltdb-shipper-cache cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space shared_store: filesystem filesystem: directory: /loki/chunks compactor: working_directory: /loki/boltdb-shipper-compactor shared_store: filesystem limits_config: reject_old_samples: true reject_old_samples_max_age: 168h chunk_store_config: max_look_back_period: 0s table_manager: retention_deletes_enabled: false retention_period: 0s ruler: storage: type: local local: directory: /loki/rules rule_path: /loki/rules-temp alertmanager_url: http://localhost:9093 ring: kvstore: store: inmemory enable_api: true
  • The PromTail configuration file/mydata/loki/promtail.ymlHere it is, again using the default configuration, which is hereclients.urlIt’s important to note, because we’re usingdocker-composeDeploy, so you can change the service namelokiTo access the Loki service as a domain name;
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://loki:3100/loki/api/v1/push

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      __path__: /var/log/*log
  • rundocker-compose.ymlThe script installs all the services using the following command;
docker-compose up -d
  • After successful operation, it can be useddocker ps |grep lpgCommand to view service status.
[root @ local - Linux LPG] # docker ps | grep LPG 64761 b407423 grafana/Loki "/ usr/bin/Loki - conf..." 3 minutes ago Up 3 minutes 0.0.0.0:3100->3100/ TCP LPG-Loki 67F0F0912971 grafana/grafana "/run.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3100->3100/ TCP LPG-Loki 67F0F0912971 grafana/grafana "/run.sh "/usr/bin/promtail - "Minutes 0.0.0.0: 1000-> 3000/ TCP lpG-grafana f2d78eb188d1 grafana/promtail" 3 minutes ago Up 3 minutes lpg-promtail

use

The next step is to use the LPG log collection system to collect the logs of the SpringBoot application, which requires little special configuration.

  • Start by creating a SpringBoot application and modifying the configuration fileapplication.yml, outputs the log to/var/logsDirectory;
spring:
  application:
    name: mall-tiny-loki

logging:
  path: /var/logs
  level:
    com.macro.mall.tiny: debug
  • Run the SpringBoot application with the following command and mount the log directory on the host so that the PromTail can collect the logs.
docker run -p 8088:8088 --name mall-tiny-loki \ -v /etc/localtime:/etc/localtime \ -v /mydata/app/mall-tiny-loki/logs:/var/logs \ -e TZ="Asia/Shanghai" \ -d mall-tiny/mall-tiny-loki: 1.0-snapshot
  • Login to Grafana with passwordadmin:admin, LOKI needs to be added as data source after successful login, and the access address is:http://192.168.7.149:3000/

  • By selecting Loki in the data source selection screen, we can see that Grafana also supports Elasticsearch as a data source.

  • Then set your Loki access address and clickSave&testSave and test, display a green message indicating successful setting, Loki access address:http://192.168.7.149:3100

  • The next inExploreSelect Loki and enter the Loki Query as{filename="/var/log/spring.log"}, you can view the log output from our SpringBoot application.

conclusion

This article describes how to build an LPG logging system and use it to collect logs from SpringBoot applications. The LPG logging collection scheme is very lightweight and has good performance! But if you need to do a full text search of your logs, you’ll still need to use the ELK system. If you’re not familiar with Grafana, check out this article titled “Next Generation Visual Surveillance System with SpringBoot”. .

The resources

  • Loki official documentation: https://grafana.com/docs/loki…
  • Promtail official documentation: https://grafana.com/docs/loki…

Project source address

https://github.com/macrozheng…

In this paper, making
https://github.com/macrozheng/mall-learningHas been included, welcome everyone STAR!