|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/ecs-logging/go-zerolog/current/setup.html |
| 4 | +navigation_title: Get started |
| 5 | +--- |
| 6 | + |
| 7 | +# Get started with ECS Logging Go (Zerolog) [setup] |
| 8 | + |
| 9 | + |
| 10 | +## Step 1: Install [setup-step-1] |
| 11 | + |
| 12 | +Add the package to your `go.mod` file: |
| 13 | + |
| 14 | +```go |
| 15 | +require go.elastic.co/ecszerolog main |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | +## Step 2: Configure [setup-step-2] |
| 20 | + |
| 21 | +Set up a default logger. For example: |
| 22 | + |
| 23 | +```go |
| 24 | +logger := ecszerolog.New(os.Stdout) |
| 25 | +log.Logger = logger |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Examples [examples] |
| 30 | + |
| 31 | + |
| 32 | +### Use structured logging [use-structured-logging] |
| 33 | + |
| 34 | +```go |
| 35 | +// Add custom fields. |
| 36 | +log.Info().Msg("hello world").Str("custom", "foo") |
| 37 | +``` |
| 38 | + |
| 39 | +The example above produces the following log output: |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "@timestamp": "2021-01-20T11:12:43.061+0800", |
| 44 | + "custom":"foo", |
| 45 | + "ecs.version": "1.6.0", |
| 46 | + "log.level": "info", |
| 47 | + "message":"hello world" |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +## Step 3: Configure Filebeat [setup-step-3] |
| 53 | + |
| 54 | +:::::::{tab-set} |
| 55 | + |
| 56 | +::::::{tab-item} Log file |
| 57 | +1. Follow the [Filebeat quick start](beats://docs/reference/filebeat/filebeat-installation-configuration.md) |
| 58 | +2. Add the following configuration to your `filebeat.yaml` file. |
| 59 | + |
| 60 | +For Filebeat 7.16+ |
| 61 | + |
| 62 | +```yaml |
| 63 | +filebeat.inputs: |
| 64 | +- type: filestream <1> |
| 65 | + paths: /path/to/logs.json |
| 66 | + parsers: |
| 67 | + - ndjson: |
| 68 | + overwrite_keys: true <2> |
| 69 | + add_error_key: true <3> |
| 70 | + expand_keys: true <4> |
| 71 | + |
| 72 | +processors: <5> |
| 73 | + - add_host_metadata: ~ |
| 74 | + - add_cloud_metadata: ~ |
| 75 | + - add_docker_metadata: ~ |
| 76 | + - add_kubernetes_metadata: ~ |
| 77 | +``` |
| 78 | +
|
| 79 | +1. Use the filestream input to read lines from active log files. |
| 80 | +2. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 81 | +3. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 82 | +4. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 83 | +5. Processors enhance your data. See [processors](beats://docs/reference/filebeat/filtering-enhancing-data.md) to learn more. |
| 84 | +
|
| 85 | +
|
| 86 | +For Filebeat < 7.16 |
| 87 | +
|
| 88 | +```yaml |
| 89 | +filebeat.inputs: |
| 90 | +- type: log |
| 91 | + paths: /path/to/logs.json |
| 92 | + json.keys_under_root: true |
| 93 | + json.overwrite_keys: true |
| 94 | + json.add_error_key: true |
| 95 | + json.expand_keys: true |
| 96 | + |
| 97 | +processors: |
| 98 | +- add_host_metadata: ~ |
| 99 | +- add_cloud_metadata: ~ |
| 100 | +- add_docker_metadata: ~ |
| 101 | +- add_kubernetes_metadata: ~ |
| 102 | +``` |
| 103 | +:::::: |
| 104 | +
|
| 105 | +::::::{tab-item} Kubernetes |
| 106 | +1. Make sure your application logs to stdout/stderr. |
| 107 | +2. Follow the [Run Filebeat on Kubernetes](beats://docs/reference/filebeat/running-on-kubernetes.md) guide. |
| 108 | +3. Enable [hints-based autodiscover](beats://docs/reference/filebeat/configuration-autodiscover-hints.md) (uncomment the corresponding section in `filebeat-kubernetes.yaml`). |
| 109 | +4. Add these annotations to your pods that log using ECS loggers. This will make sure the logs are parsed appropriately. |
| 110 | + |
| 111 | +```yaml |
| 112 | +annotations: |
| 113 | + co.elastic.logs/json.overwrite_keys: true <1> |
| 114 | + co.elastic.logs/json.add_error_key: true <2> |
| 115 | + co.elastic.logs/json.expand_keys: true <3> |
| 116 | +``` |
| 117 | + |
| 118 | +1. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 119 | +2. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 120 | +3. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 121 | +:::::: |
| 122 | + |
| 123 | +::::::{tab-item} Docker |
| 124 | +1. Make sure your application logs to stdout/stderr. |
| 125 | +2. Follow the [Run Filebeat on Docker](beats://docs/reference/filebeat/running-on-docker.md) guide. |
| 126 | +3. Enable [hints-based autodiscover](beats://docs/reference/filebeat/configuration-autodiscover-hints.md). |
| 127 | +4. Add these labels to your containers that log using ECS loggers. This will make sure the logs are parsed appropriately. |
| 128 | + |
| 129 | +```yaml |
| 130 | +labels: |
| 131 | + co.elastic.logs/json.overwrite_keys: true <1> |
| 132 | + co.elastic.logs/json.add_error_key: true <2> |
| 133 | + co.elastic.logs/json.expand_keys: true <3> |
| 134 | +``` |
| 135 | + |
| 136 | +1. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 137 | +2. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 138 | +3. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 139 | +:::::: |
| 140 | + |
| 141 | +::::::: |
| 142 | +For more information, see the [Filebeat reference](beats://docs/reference/filebeat/configuring-howto-filebeat.md). |
| 143 | + |
0 commit comments