Skip to content

[8.19] Extend default log pattern on server-side to include error information (#219940)#221339

Merged
maryam-saeidi merged 1 commit intoelastic:8.19from
maryam-saeidi:backport/8.19/pr-219940
May 23, 2025
Merged

[8.19] Extend default log pattern on server-side to include error information (#219940)#221339
maryam-saeidi merged 1 commit intoelastic:8.19from
maryam-saeidi:backport/8.19/pr-219940

Conversation

@maryam-saeidi
Copy link
Copy Markdown
Member

Backport

This will backport the following commits from main to 8.19:

Questions ?

Please refer to the Backport tool documentation

elastic#219940)

## Release Notes
Kibana logging's pattern layout, used by default for the console
appender, will now use a new default pattern layout
`[%date][%level][%logger] %message %error`. This will include the error
name and stack trace if these were included in the log entry. To opt out
of this behavior users can omit the `%error` placeholder from their log
pattern config in kibana.yml e.g.:
```
logging:
  appenders:
    console:
      type: console
      layout:
        type: pattern
        pattern: "[%date][%level][%logger] %message"
```

## Summary

Previously, when we pass the error in meta, the information related to
stacktrace and error message was not available in console. This PR
changed the default pattern to also include error information if it is
provided in meta (similar to the way that the logging happens when error
is directly passed to logger.error).

New pattern: (added `%error` at the end)
```
[%date][%level][%logger] %message %error
```

Here you can see the difference:

Logger:

```
server.logger.error(
        `Unable to create Synthetics monitor ${monitorWithNamespace[ConfigKey.NAME]}`,
        { error: e }
      );
```

#### Before

![image](https://github.com/user-attachments/assets/4f3ff751-84d5-4b5b-b6a9-d49f868a9606)

#### After

![image](https://github.com/user-attachments/assets/e22b8e45-1b0a-4d8c-b51d-5dfb3938da4f)

### Alternative
We could also change the MetaConversion and include this information,
but we might have additional meta information which I am not sure if it
is OK to be logged by default. Let me know if you prefer changing
MetaConversion instead of adding a new error conversion.

<details>
<summary>Code changes for MetaConversion</summary>

```
function isError(x: any): x is Error {
  return x instanceof Error;
}

export const MetaConversion: Conversion = {
  pattern: /%meta/g,
  convert(record: LogRecord) {
    if (!record.meta) {
      return '';
    }
    const { error, ...rest } = record.meta;
    const metaString = Object.keys(rest).length !== 0 ? JSON.stringify(rest) : '';
    let errorString = '';

    if (isError(record.meta?.error)) {
      errorString = record.meta?.error.stack || '';
    }

    return [metaString, errorString].filter(Boolean).join(' ');
  },
};
```
</details>

Here is how adjusting meta will look like in this case:

![image](https://github.com/user-attachments/assets/d7dce9bc-7147-472d-b434-373322f41bbf)

(cherry picked from commit 3d86a17)

# Conflicts:
#	docs/reference/configuration-reference/logging-settings.md
@github-actions
Copy link
Copy Markdown
Contributor

A documentation preview will be available soon.

Request a new doc build by commenting
  • Rebuild this PR: run docs-build
  • Rebuild this PR and all Elastic docs: run docs-build rebuild

run docs-build is much faster than run docs-build rebuild. A rebuild should only be needed in rare situations.

If your PR continues to fail for an unknown reason, the doc build pipeline may be broken. Elastic employees can check the pipeline status here.

@maryam-saeidi maryam-saeidi merged commit 821139a into elastic:8.19 May 23, 2025
12 checks passed
@elasticmachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
core 409 410 +1

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/core-logging-common-internal 34 38 +4

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
core 433.8KB 434.0KB +174.0B
Unknown metric groups

API count

id before after diff
@kbn/core-logging-common-internal 41 45 +4

@maryam-saeidi maryam-saeidi deleted the backport/8.19/pr-219940 branch May 23, 2025 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR is a backport of another PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants