日志存储方案

编程

app项目需要记录各种类型的日志:用户登录日志、后台操作日志等
为方便统一管理,日志统一存储至ElasticSearch中,与app业务剥离。

环境搭建 ES&Kibana

elasticsearch

docker pull elasticsearch:6.7.0

 

mkdir -p /data/elasticsearch/config

 

mkdir -p /data/elasticsearch/data

 

echo "http.host: 0.0.0.0" >> /data/elasticsearch/config/elasticsearch.yml

 

echo "network.host: 0.0.0.0" >> /data/elasticsearch/config/elasticsearch.yml

 

docker run -d --name elasticsearch6.7.0 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /data/elasticsearch/data:/usr/share/elasticsearch/data  02982be5777d

kibana

docker pull kibana:6.7.0

 

docker images

 

docker run --name kibana6.7.0 -e ELASTICSEARCH_URL=http://172.17.0.3:9200 -p 5601:5601 -d a19f604cd838

示例

约定日志索引类型&字段

PUT  platform_member_log

{

    "settings": {

        "number_of_replicas"1,

        "number_of_shards"5

    },

    "mappings" : {

      "_doc" : {

        "properties" : {

          "uid" : {

            "type" "integer"

          },

          "auth_type" : {

            "type" "keyword"

          },

          "command" : {

            "type" "keyword"

          },

          "version" : {

            "type" "double"

          },

          "client" : {

            "type" "keyword"

          },

          "device_id" : {

            "type" "keyword"

          },

          "ip" : {

            "type" "keyword"

          },

          "os" : {

            "type" "keyword"

          },

          "osver" : {

            "type" "keyword"

          },

          "error_count" : {

            "type" "integer"

          }

        }

      }

    }

  }

 

客户端链接:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html


使用es扩展 https://packagist.org/packages/cviebrock/laravel-elasticsearch

写入测试

$data = [

    "body" => [

        "uid" => 1,

        "auth_type" => "weixin",

        "command"   => "登录成功",

        "version"   => "1.0",

        "client"    => "xxx",

        "device_id" => "111222333",

        "ip"    => "127.0.0.1",

        "os"    => "ios",

        "osver" => "9.3",

        "error_count"   => 1

    ],

    "index" => "platform_member_log",

    "type" => "_doc",

    "id" => 1,

];

$client = ClientBuilder::create()->build();

return $client->index($data);

Kibana初始化

 

安装完kibana后无法访问后,查看日志:Unable to revive connection: http://elasticsearch:9200/

解决办法:进入容器,修改config/kibana.yml  中

server.host 修改为 0.0.0.0

elasticsearch.hosts 修改为 elasticSearch容器IP 

http://172.17.0.2:9200

重启容器即可

以上是 日志存储方案 的全部内容, 来源链接: utcz.com/z/511040.html

回到顶部