-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement base browser-side logging system #144107
Merged
pgayvallet
merged 21 commits into
elastic:main
from
pgayvallet:kbn-33796-browser-side-logging
Nov 1, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1d6486f
start refactoring and moving stuff around
pgayvallet a26d1d5
add abstraction for BaseLogger
pgayvallet 63968e8
plug logging into core system
pgayvallet 931c9e5
add logger factory to plugin init context
pgayvallet 000da5b
add unit tests for browser package
pgayvallet 37f676b
add more unit tests
pgayvallet 730ea22
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 80fb9a2
fix mock
pgayvallet 5c480e0
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet c16eeb5
add mocks, update system tests
pgayvallet 1ee8dc1
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet 066b87e
update files due to merge
pgayvallet 94818ac
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine f59e2d0
update readme and package
pgayvallet 75b796a
remove chalk usages from client-side
pgayvallet 1564d05
add plugin context tests
pgayvallet 2fc7ff3
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet e7ff0b0
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet d898143
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 6fdd727
fix new packages
pgayvallet a76fdd5
Merge branch 'main' into kbn-33796-browser-side-logging
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/core/logging/core-logging-browser-internal/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
load("@npm//@bazel/typescript:index.bzl", "ts_config") | ||
load("@build_bazel_rules_nodejs//:index.bzl", "js_library") | ||
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project") | ||
|
||
PKG_DIRNAME = "core-logging-browser-internal" | ||
PKG_REQUIRE_NAME = "@kbn/core-logging-browser-internal" | ||
|
||
SOURCE_FILES = glob( | ||
[ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
exclude = [ | ||
"**/*.config.js", | ||
"**/*.mock.*", | ||
"**/*.test.*", | ||
"**/*.stories.*", | ||
"**/__snapshots__/**", | ||
"**/integration_tests/**", | ||
"**/mocks/**", | ||
"**/scripts/**", | ||
"**/storybook/**", | ||
"**/test_fixtures/**", | ||
"**/test_helpers/**", | ||
], | ||
) | ||
|
||
SRCS = SOURCE_FILES | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = SRCS, | ||
) | ||
|
||
NPM_MODULE_EXTRA_FILES = [ | ||
"package.json", | ||
] | ||
|
||
RUNTIME_DEPS = [ | ||
"//packages/core/logging/core-logging-common-internal", | ||
] | ||
|
||
TYPES_DEPS = [ | ||
"@npm//@types/node", | ||
"@npm//@types/jest", | ||
"//packages/kbn-logging:npm_module_types", | ||
"//packages/core/logging/core-logging-common-internal:npm_module_types", | ||
] | ||
|
||
jsts_transpiler( | ||
name = "target_node", | ||
srcs = SRCS, | ||
build_pkg_name = package_name(), | ||
) | ||
|
||
jsts_transpiler( | ||
name = "target_web", | ||
srcs = SRCS, | ||
build_pkg_name = package_name(), | ||
web = True, | ||
) | ||
|
||
ts_config( | ||
name = "tsconfig", | ||
src = "tsconfig.json", | ||
deps = [ | ||
"//:tsconfig.base.json", | ||
"//:tsconfig.bazel.json", | ||
], | ||
) | ||
|
||
ts_project( | ||
name = "tsc_types", | ||
args = ['--pretty'], | ||
srcs = SRCS, | ||
deps = TYPES_DEPS, | ||
declaration = True, | ||
emit_declaration_only = True, | ||
out_dir = "target_types", | ||
tsconfig = ":tsconfig", | ||
) | ||
|
||
js_library( | ||
name = PKG_DIRNAME, | ||
srcs = NPM_MODULE_EXTRA_FILES, | ||
deps = RUNTIME_DEPS + [":target_node", ":target_web"], | ||
package_name = PKG_REQUIRE_NAME, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
js_library( | ||
name = "npm_module_types", | ||
srcs = NPM_MODULE_EXTRA_FILES, | ||
deps = RUNTIME_DEPS + [":target_node", ":target_web", ":tsc_types"], | ||
package_name = PKG_REQUIRE_NAME, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
pkg_npm( | ||
name = "npm_module", | ||
deps = [":" + PKG_DIRNAME], | ||
) | ||
|
||
filegroup( | ||
name = "build", | ||
srcs = [":npm_module"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
pkg_npm( | ||
name = "build_types", | ||
deps = [":npm_module_types"], | ||
visibility = ["//visibility:public"], | ||
) |
3 changes: 3 additions & 0 deletions
3
packages/core/logging/core-logging-browser-internal/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @kbn/core-logging-browser-internal | ||
|
||
This package contains the internal types and implementation for Core's browser-side logging service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { BaseLogger, BrowserLoggingSystem, type IBrowserLoggingSystem } from './src'; |
13 changes: 13 additions & 0 deletions
13
packages/core/logging/core-logging-browser-internal/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../../../..', | ||
roots: ['<rootDir>/packages/core/logging/core-logging-browser-internal'], | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/core/logging/core-logging-browser-internal/kibana.jsonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/core-logging-browser-internal", | ||
"owner": "@elastic/kibana-core", | ||
"runtimeDeps": [], | ||
"typeDeps": [], | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/core/logging/core-logging-browser-internal/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@kbn/core-logging-browser-internal", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "./target_node/index.js", | ||
"browser": "./target_web/index.js", | ||
"author": "Kibana Core", | ||
"license": "SSPL-1.0 OR Elastic License 2.0", | ||
"types": "./target_types/index.d.ts" | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/core/logging/core-logging-browser-internal/src/appenders/console_appender.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { LogRecord, LogLevel } from '@kbn/logging'; | ||
import { ConsoleAppender } from './console_appender'; | ||
|
||
test('`append()` correctly formats records and pushes them to console.', () => { | ||
jest.spyOn(global.console, 'log').mockImplementation(() => { | ||
// noop | ||
}); | ||
|
||
const records: LogRecord[] = [ | ||
{ | ||
context: 'context-1', | ||
level: LogLevel.All, | ||
message: 'message-1', | ||
timestamp: new Date(), | ||
pid: 5355, | ||
}, | ||
{ | ||
context: 'context-2', | ||
level: LogLevel.Trace, | ||
message: 'message-2', | ||
timestamp: new Date(), | ||
pid: 5355, | ||
}, | ||
{ | ||
context: 'context-3', | ||
error: new Error('Error'), | ||
level: LogLevel.Fatal, | ||
message: 'message-3', | ||
timestamp: new Date(), | ||
pid: 5355, | ||
}, | ||
]; | ||
|
||
const appender = new ConsoleAppender({ | ||
format(record) { | ||
return `mock-${JSON.stringify(record)}`; | ||
}, | ||
}); | ||
|
||
for (const record of records) { | ||
appender.append(record); | ||
// eslint-disable-next-line no-console | ||
expect(console.log).toHaveBeenCalledWith(`mock-${JSON.stringify(record)}`); | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
expect(console.log).toHaveBeenCalledTimes(records.length); | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/core/logging/core-logging-browser-internal/src/appenders/console_appender.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { Layout, LogRecord, DisposableAppender } from '@kbn/logging'; | ||
|
||
/** | ||
* | ||
* Appender that formats all the `LogRecord` instances it receives and logs them via built-in `console`. | ||
* @internal | ||
*/ | ||
export class ConsoleAppender implements DisposableAppender { | ||
/** | ||
* Creates ConsoleAppender instance. | ||
* @param layout Instance of `Layout` sub-class responsible for `LogRecord` formatting. | ||
*/ | ||
constructor(private readonly layout: Layout) {} | ||
|
||
/** | ||
* Formats specified `record` and logs it via built-in `console`. | ||
* @param record `LogRecord` instance to be logged. | ||
*/ | ||
public append(record: LogRecord) { | ||
// eslint-disable-next-line no-console | ||
console.log(this.layout.format(record)); | ||
} | ||
|
||
/** | ||
* Disposes `ConsoleAppender`. | ||
*/ | ||
public dispose() { | ||
// noop | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/core/logging/core-logging-browser-internal/src/appenders/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { ConsoleAppender } from './console_appender'; |
10 changes: 10 additions & 0 deletions
10
packages/core/logging/core-logging-browser-internal/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { BaseLogger } from './logger'; | ||
export { BrowserLoggingSystem, type IBrowserLoggingSystem } from './logging_system'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MVP implements
.log
by default, which is fine to start with.In #144276 we'd have a bit more flexibility in how we approach a fuller feature set. Assuming we'd expand on wrapping the console and, since the specifics of how the console object depends on the browser, we should at least implement a subset of the de facto methods that log to std out:
.error
,.debug
,.warn
,.info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea. I didn't want to go into browser implementation details in the first version. I'll add a bullet point in #144276 to look at how it would be possible to have a proper browser console delegation using more methods