Skip to content

Commit

Permalink
Implement base browser-side logging system (#144107)
Browse files Browse the repository at this point in the history
* start refactoring and moving stuff around

* add abstraction for BaseLogger

* plug logging into core system

* add logger factory to plugin init context

* add unit tests for browser package

* add more unit tests

* [CI] Auto-commit changed files from 'node scripts/generate codeowners'

* fix mock

* add mocks, update system tests

* update files due to merge

* [CI] Auto-commit changed files from 'node scripts/generate codeowners'

* update readme and package

* remove chalk usages from client-side

* add plugin context tests

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

* fix new packages

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
pgayvallet and kibanamachine committed Nov 1, 2022
1 parent fd6047b commit 2fb12fb
Show file tree
Hide file tree
Showing 80 changed files with 2,756 additions and 129 deletions.
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"],
)
3 changes: 3 additions & 0 deletions packages/core/logging/core-logging-browser-internal/README.md
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.
9 changes: 9 additions & 0 deletions packages/core/logging/core-logging-browser-internal/index.ts
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 packages/core/logging/core-logging-browser-internal/jest.config.js
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": [],
}
10 changes: 10 additions & 0 deletions packages/core/logging/core-logging-browser-internal/package.json
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));
}

/**
* 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';
10 changes: 10 additions & 0 deletions packages/core/logging/core-logging-browser-internal/src/index.ts
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

0 comments on commit 2fb12fb

Please sign in to comment.