如何解析 laravel 流利的日志

how to parse laravel logs in fluentd

请帮助我处理 GCP 中的 stackdriver 日志。我正在使用流利的代理将日志从服务器发送到 GCP stackdriver 日志。主要问题是我无法按级别(信息、警告、错误)拆分日志并发送正确的 fromat。 这是我的 fluentd 配置。

<source>
  @type tail
  format /^(?<message>(?<time>[^ ]*\s*[^ ]) .*)$/
  path /var/www/example.com/shared/storage/logs/laravel.*.%Y-%m-%d.log
  pos_file /var/lib/google-fluentd/pos/example-app.pos
  read_from_head true
  tag example-app
</source>

现在有些日志正在进入 stackdriver,有些还没有。如何正确地将 laravel 应用程序日志解析到 stackdriver?谢谢。

这是我尝试发送到 stackdriver 的几个日志示例

信息

[2019-06-10 17:41:03] production.INFO: Updated status for  Application with external id [ 123-456-789 ]. PubSub message: array (
  'message' => 
  array (
    'attributes' => 
    array (
      'event' => 'application.applied',
      'id' => '1111-222222-5555',
      'example_id' => '1234567890',
      'source' => 'projectname',
      'timestamp' => '1560181263',
    ),
    'data' => 'sldjfhskjdfnakjfhawejflaskdflawiefjalskdfoawiejfslKDFjlkfsjgaoiwefjawoiejflKJF',
    'messageId' => '123123123',
    'message_id' => '123123123',
    'publishTime' => '2019-06-10T15:41:03.282Z',
    'publish_time' => '2019-06-10T15:41:03.282Z',
  ),
  'subscription' => 'projects/some-name/subscriptions/example-prod-application',
  '/pubsub/projectname/application/12jh3g1j2h3g12h3g1j2h3123h' => '',
)

错误

[2019-06-10 17:33:05] production.ERROR: BraintreeException. account_id: 123123. 
 Declined. 
 Braintree\Result\Error[errors=, params=paymentMethod=customerId=123123, paymentMethodNonce=tokencc_bd_123123123, options=makeDefault=true, billingAddress=streetAddress=123123 E st apt 1111, locality=city, postalCode=123123, region=some-region, countryCodeAlpha2=NO, merchantId=123123123, message=Declined, verification=Braintree\Result\CreditCardVerification[status=processor_declined, cvvResponseCode=1233, avsErrorResponseCode=, avsPostalCodeResponseCode=U, =]

警告

 [2019-06-10 09:03:11] production.WARNING: projectname PubSub message validation error: Event for application with external id [ 123123123123 ] discarded because a later message was already processed. PubSub message: array (
  'message' => 
  array (
    'attributes' => 
    array (
      'event' => 'application.applied',
      'id' => '123123123',
      'projectname_id' => '123123123',
      'source' => 'some-source',
      'timestamp' => '1560150185',
    ),
    'data' => 'j1h2g3j1h2g3j1h23gj1h2g3j1h23g1j2h3g',
    'messageId' => '123123123',
    'message_id' => '123123123',
    'publishTime' => '2019-06-10T07:03:10.647Z',
    'publish_time' => '2019-06-10T07:03:10.647Z',
  ),
  'subscription' => 'projects/project-name/subscriptions/example-prod-application',
  '/pubsub/projectname/application/123123123123' => '',
)   
[2019-06-10 15:13:22] production.WARNING: Wrong account [ 123123123 ] provided for resetting password with token [ 123123123 ]. No password reset token found.

我通过将格式更改为 "none"

解决了这个问题
<source>
  @type tail
  format none
  path /var/www/example.com/shared/storage/logs/laravel.*.%Y-%m-%d.log
  pos_file /var/lib/google-fluentd/pos/example-app.pos
  read_from_head true
  tag example-app
</source>

对于在 GCP 中使用 fluentd 和 stackdriver 日志的 Laravel 应用程序,您可以使用格式为正则表达式的配置,如下所示:

<source>
    @type tail
    format /^\[(?<time>\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})\]\s(?<context>[^ .]+.(?<severity>\w+):(?<message>.*)$)/
    # The path of the log file.
    path /var/www/example.com/shared/storage/logs/laravel-%Y-%m-%d.log
    pos_file /var/lib/google-fluentd/pos/example-app.pos
    read_from_head true
    tag example-app
</source>

我已经测试过了。它的工作就像一个魅力。