Docker 搭建 ELK 收集并展示 tomcat 日志金沙官网线上

架构

前端展示 --> 索引搜索 <-- 日志提取及过滤 --> 日志缓存 <-- 日志收集
Kibana --> Elastash <-- Logstash --> redis <-- filebeat

操作系统: CentOS 7.4
相关软件:filebeat-6.3.0-linux-x86_64.tar.gz, docker 18.03.1-ce, redis_version:4.0.10, docker-compose 1.18.0

日志文件名称及内容:

/iba/ibaboss/java/bossmobile-tomcat-8.0.26/logs/catalina.out
#截取的内容:
22-Jun-2018 17:45:22.397 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.26
22-Jun-2018 17:45:22.399 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Aug 18 2015 11:38:37 UTC
22-Jun-2018 17:45:22.399 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.26.0

/iba/ibaboss/java/bossmobile-tomcat-8.0.26/logs/ibalife.log
# 截取的内容:
[ERROR] [2018-06-30 17:41:56][com.iba.boss.pubsub.listener.core.ListenerTemplate]ErpCustomerRegEventListener onListen Done
[ERROR] [2018-06-30 17:41:56][com.iba.boss.pubsub.listener.user.BmcLevelDescEventListener]bmcLevelDescEventListener -> Waiting for set levelDesc
[ERROR] [2018-06-30 17:41:56][com.iba.boss.pubsub.listener.core.ListenerTemplate]BmcLevelDescEventListener onListen Done

Docker 容器日志集中 ELK

安装 docker

详情可以参考 
https://www.cnblogs.com/klvchen/p/8468855.html
https://www.cnblogs.com/klvchen/p/9098745.html

ELK 基于 ovr 网络下

安装 docker-compose

详情可以参考 https://www.cnblogs.com/klvchen/p/9242774.html

金沙官网线上 1

安装 redis (这里使用 docker)

docker pull redis 

mkdir /home/ibaboss/compose/config -p 
cd  /home/ibaboss/compose/config

# redis 的配置,密码为 ibalife
vi redis.conf 

#daemonize yes
pidfile /data/redis.pid
port 6379
tcp-backlog 30000
timeout 0
tcp-keepalive 10
loglevel notice
logfile /data/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass ibalife
maxclients 30000
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events KEA
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 1000
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

# 编写 docker-compose redis yml 文件
cd /home/ibaboss/compose

vi docker-compose-redis.yml 
version: '3'
services:
  elk_redis:
    image: redis:latest
    container_name: elk_redis
    ports:
      - "192.168.0.223:6379:6379"     # 为提升安全,redis只对内网开放
    volumes:
      - ./config/redis.conf:/usr/local/etc/redis/redis.conf
    networks:
      - logs_elk  # 使用指定的网络 logs_elk
    entrypoint:
      - redis-server
      - /usr/local/etc/redis/redis.conf

networks:
  logs_elk:
    external:    # 指定使用网络
      name: logs_elk

# 创建 elk 专用的网络
docker network create  --attachable logs_elk

# 启动 redis
docker-compose -f docker-compose-redis.yml up -d 

# 查看状态
docker ps -a

# 可通过上一步获得 CONTAINER ID,查看启动日志
docker logs -f 4841efd2e1ef

docker-compose.yaml

安装 filebeat

mkdir /home/tools -p

cd /home/tools

# 安装包上传到 /home/tools
tar zxvf filebeat-6.3.0-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -s /usr/local/filebeat-6.3.0-linux-x86_64 /usr/local/filebeat
version: '2'
networks:
  network-test:
    external:
      name: ovr0
services:
  elasticsearch:
    image: elasticsearch
    network-test:
      external:
    hostname: elasticsearch
    container_name: elasticsearch
    restart: always
    volumes:
      - /opt/elasticsearch/data:/usr/share/elasticsearch/data

  kibana:
    image: kibana
    network-test:
      external:    
    hostname: kibana
    container_name: kibana
    restart: always
    environment:
      ELASTICSEARCH_URL: http://elasticsearch:9200/
    ports:
      - 5601:5601

  logstash:
    image: logstash
    network-test:
      external:
    hostname: logstash
    container_name: logstash
    restart: always
    volumes:
      - /opt/logstash/conf:/opt/logstash/conf
    command: logstash -f /opt/logstash/conf/

  filebeat:
    image: prima/filebeat
    network-test:
      external:
    hostname: filebeat
    container_name: filebeat
    restart: always
    volumes:
      - /opt/filebeat/conf/filebeat.yml:/filebeat.yml
      - /opt/upload:/data/logs
      - /opt/filebeat/registry:/etc/registry

配置 filebeat 配置文件

cd /usr/local/filebeat

cat filebeat4bossmobile.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /iba/ibaboss/java/bossmobile-tomcat-8.0.26/logs/catalina.out
  multiline.pattern: '^[[:word:]]|^java' # 匹配多行时指定正则表达式
  multiline.negate: true # 定义上边pattern匹配到的行是否用于多行合并,也就是定义是不是作为日志的一部分,multiline 规则参考文章底下链接
  multiline.match: after # 定义如何将匹配到的行组合成,在之前或者之后
  fields:      # 在采集的信息中添加一个自定义字段 service,里面的值为 bossmobile_catalina,区分两类日志
    service: bossmobile_catalina

- type: log
  enabled: true
  paths:
    - /iba/ibaboss/java/bossmobile-tomcat-8.0.26/logs/ibalife.*
  multiline.pattern: '^['
  multiline.negate: true
  multiline.match: after
  fields:      # 在采集的信息中添加一个自定义字段 service,里面的值为 bossmobile_ibalife,区分两类日志
    service: bossmobile_ibalife

output.redis:
  hosts: ["192.168.0.223"]               # 这里是 redis 的内网地址
  password: "ibalife"
  key: "bossmobile"                      # 存入到 redis 中的 bossmobile key 中
  db: 0
  timeout: 5

filebeat说明:
filebeat.yml 挂载为 filebeat 的配置文件
logs 为 容器挂载日志的目录
registry 读取日志的记录,防止filebeat 容器挂掉,需要重新读取所有日志

启动 filebeat

# 创建 filebeat 保存日志的文件夹
mkdir /iba/ibaboss/filebeat_logs

nohup ./filebeat -e -c filebeat4bossmobile.yml >/iba/ibaboss/filebeat_logs/filebeat4bossmobile.log 2>&1 & 

# 如果想重新读取日志,可以停止 filebeat 后删除,再重新启动即可
ps -ef|grep filebeat

kill -9 PID

rm /usr/local/filebeat/data/registry

logstash 配置文件如下:

安装配置 ELK

cd /home/ibaboss/compose

cat docker-compose-elk.yml 
version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
    container_name: logs_elasticsearch           # 给容器命名
    restart: always
    environment:
      - discovery.type=single-node
      - cluster.name=docker-cluster
      - network.host=0.0.0.0
      - discovery.zen.minimum_master_nodes=1
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
    networks:
      logs_elk:     # 指定使用的网络
        aliases:
          - elasticsearch     # 该容器的别名,在 logs_elk 网络中的其他容器可以通过别名 elasticsearch 来访问到该容器

  kibana:
    image: docker.elastic.co/kibana/kibana:6.2.4
    container_name: logs_kibana
    ports:
      - "5601:5601"
    restart: always
    networks:
      logs_elk:
        aliases:
          - kibana
    environment:
      - ELASTICSEARCH_URL=http://elasticsearch:9200
      - SERVER_NAME=kibana
    depends_on:
      - elasticsearch

  logstash:
    image: docker.elastic.co/logstash/logstash:6.2.4
    container_name: logs_logstash
    restart: always
    environment:
      - LS_JAVA_OPTS=-Xmx256m -Xms256m
    volumes:
      - ./config/logstash.conf:/etc/logstash.conf
    networks:
      logs_elk:
        aliases:
          - logstash
    depends_on:
      - elasticsearch
    entrypoint:
      - logstash
      - -f
      - /etc/logstash.conf

networks:
  logs_elk:
    external:
      name: logs_elk

cd /home/ibaboss/compose/config

cat logstash.conf

input {
        redis {
                port => "6379"                                    
                host => "elk_redis"             # redis 主机是 logs_elk 网络中的 elk_redis 主机
                data_type => "list"
                key  =>  "bossmobile"           # 从 redis 中 bossmobile key 中获取数据
                password => "ibalife"
        }

}

filter {        
     mutate { # 定义去除的字段
     remove_field => ["_id","@version","_index","_score","_type","beat.hostname","beat.name","beat.version","fields.service","input.type","offset","prospector.type","source"]
    }

  if [fields][service] == "bossmobile_catalina" {
    grok {   # 匹配 message 字段中的 时间,放入自定义的 customer_time 字段中
        match => [ "message" , "(?<customer_time>%{MONTHDAY}-%{MONTH}-%{YEAR} %{HOUR}:%{MINUTE}:%{SECOND})" ]
    }
  }

  if [fields][service] == "bossmobile_ibalife" {
    grok {
        match => [ "message" , "(?<customer_time>%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{HOUR}:%{MINUTE}:%{SECOND})" ]
    }
  }

    date {
       match => [ "customer_time", "dd-MMM-yyyy HH:mm:ss.SSS","yyyy-MM-dd HH:mm:ss" ]     # 格式化 customer_time 中的时间类型从string 变成 date,例如 22-Jun-2018 17:45:22.397,对应为 dd-MMM-yyyy HH:mm:ss.SSS
        locale => "en"
        target => [ "@timestamp" ]  # 替换 @timestamp 字段的值,@timestamp 的值用于 kibana 排序
        timezone => "Asia/Shanghai"
    }

}

output {  # 根据 redis 中的 service 的字段,分别创建不同的 elasticsearch index
  if [fields][service] == "bossmobile_catalina" {         
        elasticsearch {
                hosts => ["elasticsearch:9200"]
                index   => "bossmobile_catalina-%{+YYYY.MM.dd}"
        }
  }

  if [fields][service] == "bossmobile_ibalife" {
        elasticsearch {
                hosts => ["elasticsearch:9200"]
                index   => "bossmobile_ibalife-%{+YYYY.MM.dd}"
        }
  }

}

# 启动容器
cd /home/ibaboss/compose
docker-compose -f docker-compose-elk.yml  up -d

docker ps -a

第一种: 使用 patterns .

访问 kibana 所在的 ip:5061,创建 Index Patterns, bossmobile_catalina-* 和 bossmobile_ibalife-* 金沙官网线上 2


logstash.conf: (配置了二种输入模式, filebeats, syslog)   

参考

#grok 在线调试  
http://grok.qiexun.net/

# multiline 官方说明
https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html

# kibana 中文用户手册
https://www.elastic.co/guide/cn/kibana/current/index.html

https://elasticsearch.cn/question/2651
http://www.importnew.com/27705.html
https://www.elastic.co/guide/en/logstash/current/index.html
https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html
input {
    beats {
        port => 5044
        type => beats
    }

    tcp {
        port => 5000
        type => syslog
    }

}

filter {
  if[type] == "tomcat-log" {
    multiline {
      patterns_dir => "/opt/logstash/conf/patterns"
      pattern => "(^%{TOMCAT_DATESTAMP})|(^%{CATALINA_DATESTAMP})"
      negate => true
      what => "previous"
    }

    if "ERROR" in [message] {    #如果消息里有ERROR字符则将type改为自定义的标记
        mutate { replace => { type => "tomcat_catalina_error" } }
    }

    else if "WARN" in [message] {
        mutate { replace => { type => "tomcat_catalina_warn" } }
    }

    else if "DEBUG" in [message] {
        mutate { replace => { type => "tomcat_catalina_debug" } }
    }

    else {
        mutate { replace => { type => "tomcat_catalina_info" } }
    }

    grok{
      patterns_dir => "/opt/logstash/conf/patterns"
      match => [ "message", "%{TOMCATLOG}", "message", "%{CATALINALOG}" ]
      remove_field => ["message"]    #这表示匹配成功后是否删除原始信息,这个看个人情况,如果为了节省空间可以考虑删除
    }
    date {
      match => [ "timestamp", "yyyy-MM-dd HH:mm:ss,SSS Z", "MMM dd, yyyy HH:mm:ss a" ]
    }
  }    

  if[type] == "nginx-log" {

    if '"status":"404"' in [message] {
        mutate { replace => { type => "nginx_error_404" } }
    }

    else if '"status":"500"' in [message] {
        mutate { replace => { type => "nginx_error_500" } }
    }

    else if '"status":"502"' in [message] {
        mutate { replace => { type => "nginx_error_502" } }
    }

    else if '"status":"403"' in [message] {
        mutate { replace => { type => "nginx_error_403" } }
    }

    else if '"status":"504"' in [message] {
        mutate { replace => { type => "nginx_error_504" } }
    }

    else if '"status":"200"' in [message] {
        mutate { replace => { type => "nginx_200" } }
    }
    grok {
      remove_field => ["message"]    #这表示匹配成功后是否删除原始信息,这个看个人情况,如果为了节省空间可以考虑删除
    }
  }
}

output {
    elasticsearch { 
        hosts => ["elasticsearch:9200"]
    }

    #stdout { codec => rubydebug }    #输出到屏幕上
}

/opt/logstash/conf/patterns  下面存放 grok 文件

grok-patterns:

USERNAME [a-zA-Z0-9._-]+
USER %{USERNAME}
INT (?:[+-]?(?:[0-9]+))
BASE10NUM (?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:.[0-9]+)?)|(?:.[0-9]+)))
NUMBER (?:%{BASE10NUM})
BASE16NUM (?<![0-9A-Fa-f])(?:[+-]?(?:0x)?(?:[0-9A-Fa-f]+))
BASE16FLOAT b(?<![0-9A-Fa-f.])(?:[+-]?(?:0x)?(?:(?:[0-9A-Fa-f]+(?:.[0-9A-Fa-f]*)?)|(?:.[0-9A-Fa-f]+)))b

POSINT b(?:[1-9][0-9]*)b
NONNEGINT b(?:[0-9]+)b
WORD bw+b
NOTSPACE S+
SPACE s*
DATA .*?
GREEDYDATA .*
QUOTEDSTRING (?>(?<!\)(?>"(?>\.|[^\"]+)+"|""|(?>'(?>\.|[^\']+)+')|''|(?>`(?>\.|[^\`]+)+`)|``))
UUID [A-Fa-f0-9]{8}-(?:[A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}

# Networking
MAC (?:%{CISCOMAC}|%{WINDOWSMAC}|%{COMMONMAC})
CISCOMAC (?:(?:[A-Fa-f0-9]{4}.){2}[A-Fa-f0-9]{4})
WINDOWSMAC (?:(?:[A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2})
COMMONMAC (?:(?:[A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2})
IPV6 ((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?
IPV4 (?<![0-9])(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))(?![0-9])
IP (?:%{IPV6}|%{IPV4})
HOSTNAME b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(.?|b)
HOST %{HOSTNAME}
IPORHOST (?:%{HOSTNAME}|%{IP})
HOSTPORT (?:%{IPORHOST=~/./}:%{POSINT})

# paths
PATH (?:%{UNIXPATH}|%{WINPATH})
UNIXPATH (?>/(?>[w_%!$@:.,-]+|\.)*)+
TTY (?:/dev/(pts|tty([pq])?)(w+)?/?(?:[0-9]+))
WINPATH (?>[A-Za-z]+:|\)(?:\[^\?*]*)+
URIPROTO [A-Za-z]+(+[A-Za-z+]+)?
URIHOST %{IPORHOST}(?::%{POSINT:port})?
# uripath comes loosely from RFC1738, but mostly from what Firefox
# doesn't turn into %XX
URIPATH (?:/[A-Za-z0-9$.+!*'(){},~:;=@#%_-]*)+
#URIPARAM ?(?:[A-Za-z0-9]+(?:=(?:[^&]*))?(?:&(?:[A-Za-z0-9]+(?:=(?:[^&]*))?)?)*)?
URIPARAM ?[A-Za-z0-9$.+!*'|(){},~@#%&/=:;_?-[]]*
URIPATHPARAM %{URIPATH}(?:%{URIPARAM})?
URI %{URIPROTO}://(?:%{USER}(?::[^@]*)?@)?(?:%{URIHOST})?(?:%{URIPATHPARAM})?

# Months: January, Feb, 3, 03, 12, December
MONTH b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)b
MONTHNUM (?:0?[1-9]|1[0-2])
MONTHDAY (?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])

# Days: Monday, Tue, Thu, etc...
DAY (?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)

# Years?
YEAR (?>dd){1,2}
HOUR (?:2[0123]|[01]?[0-9])
MINUTE (?:[0-5][0-9])
# '60' is a leap second in most time standards and thus is valid.
SECOND (?:(?:[0-5][0-9]|60)(?:[:.,][0-9]+)?)
TIME (?!<[0-9])%{HOUR}:%{MINUTE}(?::%{SECOND})(?![0-9])
# datestamp is YYYY/MM/DD-HH:MM:SS.UUUU (or something like it)
DATE_US %{MONTHNUM}[/-]%{MONTHDAY}[/-]%{YEAR}
DATE_EU %{MONTHDAY}[./-]%{MONTHNUM}[./-]%{YEAR}
ISO8601_TIMEZONE (?:Z|[+-]%{HOUR}(?::?%{MINUTE}))
ISO8601_SECOND (?:%{SECOND}|60)
TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}?
DATE %{DATE_US}|%{DATE_EU}
DATESTAMP %{DATE}[- ]%{TIME}
TZ (?:[PMCE][SD]T|UTC)
DATESTAMP_RFC822 %{DAY} %{MONTH} %{MONTHDAY} %{YEAR} %{TIME} %{TZ}
DATESTAMP_OTHER %{DAY} %{MONTH} %{MONTHDAY} %{TIME} %{TZ} %{YEAR}

# Syslog Dates: Month Day HH:MM:SS
SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME}
PROG (?:[w._/%-]+)
SYSLOGPROG %{PROG:program}(?:[%{POSINT:pid}])?
SYSLOGHOST %{IPORHOST}
SYSLOGFACILITY <%{NONNEGINT:facility}.%{NONNEGINT:priority}>
HTTPDATE %{MONTHDAY}/%{MONTH}/%{YEAR}:%{TIME} %{INT}

# Shortcuts
QS %{QUOTEDSTRING}

# Log formats
SYSLOGBASE %{SYSLOGTIMESTAMP:timestamp} (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:logsource} %{SYSLOGPROG}:
COMMONAPACHELOG %{IPORHOST:clientip} %{USER:ident} %{USER:auth} [%{HTTPDATE:timestamp}] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)
COMBINEDAPACHELOG %{COMMONAPACHELOG} %{QS:referrer} %{QS:agent}

# Log Levels
LOGLEVEL ([A-a]lert|ALERT|[T|t]race|TRACE|[D|d]ebug|DEBUG|[N|n]otice|NOTICE|[I|i]nfo|INFO|[W|w]arn?(?:ing)?|WARN?(?:ING)?|[E|e]rr?(?:or)?|ERR?(?:OR)?|[C|c]rit?(?:ical)?|CRIT?(?:ICAL)?|[F|f]atal|FATAL|[S|s]evere|SEVERE|EMERG(?:ENCY)?|[Ee]merg(?:ency)?)

# Java Logs
JAVATHREAD (?:[A-Z]{2}-Processor[d]+)
JAVACLASS (?:[a-zA-Z0-9-]+.)+[A-Za-z0-9$]+
JAVAFILE (?:[A-Za-z0-9_.-]+)
JAVASTACKTRACEPART at %{JAVACLASS:class}.%{WORD:method}(%{JAVAFILE:file}:%{NUMBER:line})
JAVALOGMESSAGE (.*)
# MMM dd, yyyy HH:mm:ss eg: Jan 9, 2014 7:13:13 AM
CATALINA_DATESTAMP %{MONTH} %{MONTHDAY}, 20%{YEAR} %{HOUR}:?%{MINUTE}(?::?%{SECOND}) (?:AM|PM)
# yyyy-MM-dd HH:mm:ss,SSS ZZZ eg: 2014-01-09 17:32:25,527 -0800
TOMCAT_DATESTAMP 20%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{HOUR}:?%{MINUTE}(?::?%{SECOND}) %{ISO8601_TIMEZONE}
CATALINALOG %{CATALINA_DATESTAMP:timestamp} %{JAVACLASS:class} %{JAVALOGMESSAGE:logmessage}
# 2014-01-09 20:03:28,269 -0800 | ERROR | com.example.service.ExampleService - something compeletely unexpected happened...
TOMCATLOG %{TOMCAT_DATESTAMP:timestamp} | %{LOGLEVEL:level} | %{JAVACLASS:class} - %{JAVALOGMESSAGE:logmessage}

下面我们配置 容器日志输出:

docker里,标准的日志方式是用Stdout, docker 里面配置标准输出,只需要指定: syslog 就可以了。

对于 stdout 标准输出的 docker ��志,我们使用 logstash 来收集日志就可以。

我们在 docker-compose 中配置如下既可:

      logging:
        driver: syslog
        options:
          syslog-address: 'tcp://logstash:5000'

但是一般来说我们都是文件日志,那么我们就可以直接用filebeat

对于 filebeat 我们使用 官方的 dockerhub 的 prima/filebeat 镜像。

官方的镜像中,我们需要编译一个filebeat.yml 文件, 官方说明中有两种方案:

第一是 -v 挂载 -v /path/filebeat.yml:/filebeat.yml

第二是 dockerfile 的时候

FROM prima/filebeat

COPY filebeat.yml /filebeat.yml

编译一个 filebeat.yml 文件。

filebeat.yml 支持单一路径的 prospector, 也支持多个 prospector或者每个prospector多个路径。

本文由金沙官网线上发布于操作系统,转载请注明出处:Docker 搭建 ELK 收集并展示 tomcat 日志金沙官网线上

您可能还会对下面的文章感兴趣: