如何在流畅的

时间:2018-06-11 05:34:54

标签: elasticsearch logging fluentd

我在我的计算机上设置了elasticsearchfluentd。我编写了一个基本配置文件,它监视日志文件并将日志发送到elasticsearch。以下是配置文件:

<source>
  @type tail
  path /home/user/log.json
  pos_file /home/user/log.json.pos
  format json
  time_format %Y-%m-%d %H:%M:%S
  tag first
</source>

<match *first*>
  @type elasticsearch
  hosts 192.168.60.118:9200
  user <username>
  password <password>
</match>

以下是elasticsearch上收到的日志消息:

{
  "_index": "fluentd",
  "_type": "fluentd",
  "_id": "2987",
  "_version": 88,
  "_score": null,
  "_source": {
    "DataNumber": "030",
    "DataId": "MMX56",
    "DataCount": 87,
    "Status": "Done"
  },
  "fields": {
     "Created": [
      "2018-06-11T05:27:20.278Z"
     ]
  },
  "sort": [
    1528694840278
  ]
}

通过上述消息,我还想添加其他信息,例如生成此数据的machine numberfloor number和其他内容。我无法在生成这些日志的代码中对此详细信息进行硬编码。所以我正在寻找一种方法在简洁的配置文件中添加这些详细信息,以便每个到elasticsearch的出站邮件附加machine number&amp; floor number

由于

1 个答案:

答案 0 :(得分:0)

看看record

import asyncio
import time 
import datetime
from random import random

sema = asyncio.Semaphore(2)

async def my_worker():
    async with sema:
        print("{}".format(datetime.datetime.now()))
        print("I'm going to fetch some data")
        result = await data_fetcher()

        print("I'm going to save data to disk")
        await write_result_to_disk(result)

        if random() > 0.5:
            print("I need to create and run a new worker here to fetch more data")

async def data_fetcher():
    await asyncio.sleep(3)
    return "Bla bla bla"

async def write_result_to_disk(result):
    await asyncio.sleep(3)
    print(result)

blah = [my_worker(), my_worker(), my_worker(), my_worker(), my_worker()]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*blah))
print("All Workers Completed")
loop.close()