|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/ecs-logging/php/current/setup.html |
| 4 | +navigation_title: Get started |
| 5 | +--- |
| 6 | + |
| 7 | +# Get started with ECS Logging PHP [setup] |
| 8 | + |
| 9 | +::::{note} |
| 10 | +ECS logging for PHP is currently only available for Monolog v2.*. |
| 11 | +:::: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Step 1: Set up application logging [setup-step-1] |
| 16 | + |
| 17 | + |
| 18 | +### Add the dependency [_add_the_dependency] |
| 19 | + |
| 20 | +```cmd |
| 21 | +composer require elastic/ecs-logging |
| 22 | +``` |
| 23 | + |
| 24 | + |
| 25 | +### Configure monolog logger [_configure_monolog_logger] |
| 26 | + |
| 27 | +`Elastic\Monolog\v2\Formatter\ElasticCommonSchemaFormatter` implements Monolog’s [`FormatterInterface`](https://github.com/Seldaek/monolog/blob/2.0.0/src/Monolog/Formatter/FormatterInterface.php) and thus it can be used when setting up Monolog logger. |
| 28 | + |
| 29 | +For example: |
| 30 | + |
| 31 | +```php |
| 32 | +use Monolog\Logger; |
| 33 | +use Monolog\Handler\StreamHandler; |
| 34 | +use Elastic\Monolog\Formatter\ElasticCommonSchemaFormatter; |
| 35 | + |
| 36 | +$log = new Logger('MyLogger'); |
| 37 | +$handler = new StreamHandler('php://stdout', Logger::DEBUG); |
| 38 | +$handler->setFormatter(new ElasticCommonSchemaFormatter()); |
| 39 | +$log->pushHandler($handler); |
| 40 | + |
| 41 | +$log->warning('Be aware that...'); |
| 42 | +``` |
| 43 | + |
| 44 | +Logs the following JSON to standard output: |
| 45 | + |
| 46 | +```json |
| 47 | +{"@timestamp":"2021-02-07T18:08:07.229676Z","log.level":"WARNING","message":"Be aware that...","ecs.version":"1.2.0","log":{"logger":"MyLogger"}} |
| 48 | +``` |
| 49 | + |
| 50 | +Additionally, it allows for adding additional keys to messages. |
| 51 | + |
| 52 | +For example: |
| 53 | + |
| 54 | +```php |
| 55 | +$log->info('My message', ['labels' => ['my_label_key' => 'my_label_value'], 'trace.id' => 'abc-xyz']); |
| 56 | +``` |
| 57 | + |
| 58 | +Logs the following (multi-line formatted for better readability): |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "@timestamp": "2021-02-08T06:36:38.913824Z", |
| 63 | + "log.level": "INFO", |
| 64 | + "message": "My message", |
| 65 | + "ecs.version": "1.2.0", |
| 66 | + "log": { |
| 67 | + "logger": "MyLogger" |
| 68 | + }, |
| 69 | + "labels": { |
| 70 | + "my_label_key": "my_label_value" |
| 71 | + }, |
| 72 | + "trace.id": "abc-xyz" |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +## Step 2: Configure Filebeat [setup-step-2] |
| 78 | + |
| 79 | +:::::::{tab-set} |
| 80 | + |
| 81 | +::::::{tab-item} Log file |
| 82 | +1. Follow the [Filebeat quick start](beats://docs/reference/filebeat/filebeat-installation-configuration.md) |
| 83 | +2. Add the following configuration to your `filebeat.yaml` file. |
| 84 | + |
| 85 | +For Filebeat 7.16+ |
| 86 | + |
| 87 | +```yaml |
| 88 | +filebeat.inputs: |
| 89 | +- type: filestream <1> |
| 90 | + paths: /path/to/logs.json |
| 91 | + parsers: |
| 92 | + - ndjson: |
| 93 | + overwrite_keys: true <2> |
| 94 | + add_error_key: true <3> |
| 95 | + expand_keys: true <4> |
| 96 | + |
| 97 | +processors: <5> |
| 98 | + - add_host_metadata: ~ |
| 99 | + - add_cloud_metadata: ~ |
| 100 | + - add_docker_metadata: ~ |
| 101 | + - add_kubernetes_metadata: ~ |
| 102 | +``` |
| 103 | +
|
| 104 | +1. Use the filestream input to read lines from active log files. |
| 105 | +2. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 106 | +3. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 107 | +4. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 108 | +5. Processors enhance your data. See [processors](beats://docs/reference/filebeat/filtering-enhancing-data.md) to learn more. |
| 109 | +
|
| 110 | +
|
| 111 | +For Filebeat < 7.16 |
| 112 | +
|
| 113 | +```yaml |
| 114 | +filebeat.inputs: |
| 115 | +- type: log |
| 116 | + paths: /path/to/logs.json |
| 117 | + json.keys_under_root: true |
| 118 | + json.overwrite_keys: true |
| 119 | + json.add_error_key: true |
| 120 | + json.expand_keys: true |
| 121 | + |
| 122 | +processors: |
| 123 | +- add_host_metadata: ~ |
| 124 | +- add_cloud_metadata: ~ |
| 125 | +- add_docker_metadata: ~ |
| 126 | +- add_kubernetes_metadata: ~ |
| 127 | +``` |
| 128 | +:::::: |
| 129 | +
|
| 130 | +::::::{tab-item} Kubernetes |
| 131 | +1. Make sure your application logs to stdout/stderr. |
| 132 | +2. Follow the [Run Filebeat on Kubernetes](beats://docs/reference/filebeat/running-on-kubernetes.md) guide. |
| 133 | +3. Enable [hints-based autodiscover](beats://docs/reference/filebeat/configuration-autodiscover-hints.md) (uncomment the corresponding section in `filebeat-kubernetes.yaml`). |
| 134 | +4. Add these annotations to your pods that log using ECS loggers. This will make sure the logs are parsed appropriately. |
| 135 | + |
| 136 | +```yaml |
| 137 | +annotations: |
| 138 | + co.elastic.logs/json.overwrite_keys: true <1> |
| 139 | + co.elastic.logs/json.add_error_key: true <2> |
| 140 | + co.elastic.logs/json.expand_keys: true <3> |
| 141 | +``` |
| 142 | + |
| 143 | +1. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 144 | +2. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 145 | +3. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 146 | +:::::: |
| 147 | + |
| 148 | +::::::{tab-item} Docker |
| 149 | +1. Make sure your application logs to stdout/stderr. |
| 150 | +2. Follow the [Run Filebeat on Docker](beats://docs/reference/filebeat/running-on-docker.md) guide. |
| 151 | +3. Enable [hints-based autodiscover](beats://docs/reference/filebeat/configuration-autodiscover-hints.md). |
| 152 | +4. Add these labels to your containers that log using ECS loggers. This will make sure the logs are parsed appropriately. |
| 153 | + |
| 154 | +```yaml |
| 155 | +labels: |
| 156 | + co.elastic.logs/json.overwrite_keys: true <1> |
| 157 | + co.elastic.logs/json.add_error_key: true <2> |
| 158 | + co.elastic.logs/json.expand_keys: true <3> |
| 159 | +``` |
| 160 | + |
| 161 | +1. Values from the decoded JSON object overwrite the fields that {{filebeat}} normally adds (type, source, offset, etc.) in case of conflicts. |
| 162 | +2. {{filebeat}} adds an "error.message" and "error.type: json" key in case of JSON unmarshalling errors. |
| 163 | +3. {{filebeat}} will recursively de-dot keys in the decoded JSON, and expand them into a hierarchical object structure. |
| 164 | +:::::: |
| 165 | + |
| 166 | +::::::: |
| 167 | +For more information, see the [Filebeat reference](beats://docs/reference/filebeat/configuring-howto-filebeat.md). |
| 168 | + |
0 commit comments