Skip to content
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
merged 21 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
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 Oct 27, 2022
a26d1d5
add abstraction for BaseLogger
pgayvallet Oct 27, 2022
63968e8
plug logging into core system
pgayvallet Oct 27, 2022
931c9e5
add logger factory to plugin init context
pgayvallet Oct 27, 2022
000da5b
add unit tests for browser package
pgayvallet Oct 27, 2022
37f676b
add more unit tests
pgayvallet Oct 27, 2022
730ea22
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Oct 27, 2022
80fb9a2
fix mock
pgayvallet Oct 28, 2022
5c480e0
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet Oct 28, 2022
c16eeb5
add mocks, update system tests
pgayvallet Oct 28, 2022
1ee8dc1
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet Oct 28, 2022
066b87e
update files due to merge
pgayvallet Oct 28, 2022
94818ac
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Oct 28, 2022
f59e2d0
update readme and package
pgayvallet Oct 28, 2022
75b796a
remove chalk usages from client-side
pgayvallet Oct 28, 2022
1564d05
add plugin context tests
pgayvallet Oct 28, 2022
2fc7ff3
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet Oct 28, 2022
e7ff0b0
Merge remote-tracking branch 'upstream/main' into kbn-33796-browser-s…
pgayvallet Oct 31, 2022
d898143
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 31, 2022
6fdd727
fix new packages
pgayvallet Oct 31, 2022
a76fdd5
Merge branch 'main' into kbn-33796-browser-side-logging
kibanamachine Nov 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ packages/core/lifecycle/core-lifecycle-browser-mocks @elastic/kibana-core
packages/core/lifecycle/core-lifecycle-server @elastic/kibana-core
packages/core/lifecycle/core-lifecycle-server-internal @elastic/kibana-core
packages/core/lifecycle/core-lifecycle-server-mocks @elastic/kibana-core
packages/core/logging/core-logging-browser-internal @elastic/kibana-core
packages/core/logging/core-logging-browser-mocks @elastic/kibana-core
packages/core/logging/core-logging-common-internal @elastic/kibana-core
packages/core/logging/core-logging-server @elastic/kibana-core
packages/core/logging/core-logging-server-internal @elastic/kibana-core
packages/core/logging/core-logging-server-mocks @elastic/kibana-core
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@
"@kbn/core-lifecycle-server": "link:bazel-bin/packages/core/lifecycle/core-lifecycle-server",
"@kbn/core-lifecycle-server-internal": "link:bazel-bin/packages/core/lifecycle/core-lifecycle-server-internal",
"@kbn/core-lifecycle-server-mocks": "link:bazel-bin/packages/core/lifecycle/core-lifecycle-server-mocks",
"@kbn/core-logging-browser-internal": "link:bazel-bin/packages/core/logging/core-logging-browser-internal",
"@kbn/core-logging-browser-mocks": "link:bazel-bin/packages/core/logging/core-logging-browser-mocks",
"@kbn/core-logging-common-internal": "link:bazel-bin/packages/core/logging/core-logging-common-internal",
"@kbn/core-logging-server": "link:bazel-bin/packages/core/logging/core-logging-server",
"@kbn/core-logging-server-internal": "link:bazel-bin/packages/core/logging/core-logging-server-internal",
"@kbn/core-logging-server-mocks": "link:bazel-bin/packages/core/logging/core-logging-server-mocks",
Expand Down
6 changes: 6 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ filegroup(
"//packages/core/lifecycle/core-lifecycle-server:build",
"//packages/core/lifecycle/core-lifecycle-server-internal:build",
"//packages/core/lifecycle/core-lifecycle-server-mocks:build",
"//packages/core/logging/core-logging-browser-internal:build",
"//packages/core/logging/core-logging-browser-mocks:build",
"//packages/core/logging/core-logging-common-internal:build",
"//packages/core/logging/core-logging-server:build",
"//packages/core/logging/core-logging-server-internal:build",
"//packages/core/logging/core-logging-server-mocks:build",
Expand Down Expand Up @@ -463,6 +466,9 @@ filegroup(
"//packages/core/lifecycle/core-lifecycle-server:build_types",
"//packages/core/lifecycle/core-lifecycle-server-internal:build_types",
"//packages/core/lifecycle/core-lifecycle-server-mocks:build_types",
"//packages/core/logging/core-logging-browser-internal:build_types",
"//packages/core/logging/core-logging-browser-mocks:build_types",
"//packages/core/logging/core-logging-common-internal:build_types",
"//packages/core/logging/core-logging-server:build_types",
"//packages/core/logging/core-logging-server-internal:build_types",
"//packages/core/logging/core-logging-server-mocks:build_types",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
*/

import type { EnvironmentMode, PackageInfo } from '@kbn/config';
import type { LoggerFactory } from '@kbn/logging';
import type { CoreId } from '@kbn/core-base-common-internal';

/** @internal */
export interface CoreContext {
coreId: CoreId;
logger: LoggerFactory;
env: {
mode: Readonly<EnvironmentMode>;
packageInfo: Readonly<PackageInfo>;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/base/core-base-browser-mocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ NPM_MODULE_EXTRA_FILES = [
]

RUNTIME_DEPS = [
"//packages/kbn-logging-mocks",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"//packages/core/base/core-base-browser-internal:npm_module_types",
"//packages/kbn-logging-mocks:npm_module_types",
"//packages/core/base/core-base-browser-internal:npm_module_types",
]

jsts_transpiler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* Side Public License, v 1.
*/

import { loggerMock } from '@kbn/logging-mocks';
import type { CoreContext } from '@kbn/core-base-browser-internal';

function createCoreContext({ production = false }: { production?: boolean } = {}): CoreContext {
return {
coreId: Symbol('core context mock'),
logger: loggerMock.create(),
env: {
mode: {
dev: !production,
Expand Down
114 changes: 114 additions & 0 deletions packages/core/logging/core-logging-browser-internal/BUILD.bazel
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"],
)
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.
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';
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'],
};
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": [],
}
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"
}
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);
});
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));
Copy link
Contributor

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

Copy link
Contributor Author

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

}

/**
* Disposes `ConsoleAppender`.
*/
public dispose() {
// noop
}
}
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';
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';
Loading