-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Implement base browser-side logging system #144107
Conversation
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.
Self review
export { LoggerConversion } from './logger'; | ||
export { LevelConversion } from './level'; | ||
export { MessageConversion } from './message'; | ||
export { MetaConversion } from './meta'; | ||
export { DateConversion } from './date'; |
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.
I moved the pattern conversions to the common package. A few notes though:
- the
pid
conversion was kept on the server-side, given the concept cannot be used on browser side - the
level
andlogger
conversion were supporting highlighting usingchalk
. This only works with for terminal output (and was leaking server-only libraries into core's browser bundle), so I duplicated them and removed the highlight from the common version.
* color highlighting (eg. to make log messages easier to read in the terminal). | ||
* @internal | ||
*/ | ||
export class PatternLayout implements Layout { |
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.
This base PatternLayout
is inherited by the browser and server implementations.
* A basic, abstract logger implementation that delegates the create of log records to the child's createLogRecord function. | ||
* @internal | ||
*/ | ||
export abstract class AbstractLogger implements Logger { |
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.
Same logic here, I created an abstract logger class that is used for the concrete browser/server implementations. The only abstract method to implement on the children is createLogRecord
* color highlighting (eg. to make log messages easier to read in the terminal). | ||
* @internal | ||
*/ | ||
export class PatternLayout extends BasePatternLayout { |
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.
Inheriting the base PatternLayout
. Only the constructor is overridden to manually pass some parameters to it:
highlight: false
given it's not supported on the browser- the browser-side list of supported
conversions
(excluding thepid
one)
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.
Is there a reason for renaming the base pattern layout here rather than in the common code?
private getLoggerConfigByContext(context: string): LoggerConfigType { | ||
return { | ||
level: this.loggingConfig.logLevel, | ||
appenders: [CONSOLE_APPENDER_ID], | ||
name: context, | ||
}; | ||
} |
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.
As planned for the initial version, the browser-side logging configuration is very naive, and preconfigure all loggers to use the global level
and a single console
appender.
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.
As an MVP, this is fine.
export interface LoggerConfigType { | ||
appenders: string[]; | ||
name: string; | ||
level: LogLevelId; | ||
} |
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.
This is a duplication from the type in the @kbn/core-logging-server
package (packages/core/logging/core-logging-server/src/logger.ts
).
When we'll eventually want to support full browser-side logging configuration, we will need to move all the logging config types from the @kbn/core-logging-server
to a common one, but given this wasn't necessary for the first implementation, I simply copied the single type I'm using.
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.
I created #144276 as a way to track enhancements and priority for further work in this area.
logger: { | ||
get(...contextParts) { | ||
return coreContext.logger.get('plugins', pluginManifest.id, ...contextParts); | ||
}, | ||
}, |
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.
pluginInitConfig.logger.get('...')
now available on the browser-side.
const logLevel: LogLevelId = injectedMetadata.env.mode.dev ? 'all' : 'warn'; | ||
this.loggingSystem = new BrowserLoggingSystem({ logLevel }); |
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.
Using the all
level for development and warn
level for production.
Pinging @elastic/kibana-core (Team:Core) |
*/ | ||
public append(record: LogRecord) { | ||
// eslint-disable-next-line no-console | ||
console.log(this.layout.format(record)); |
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
protected readonly factory: LoggerFactory | ||
) {} | ||
|
||
protected abstract createLogRecord<Meta extends LogMeta>( |
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.
TIL (again): protected
vs private
. I had to read up on protected
vs private
class methods. The difference is subtle.
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 almost never use protected
in typescript, but for abstract class, the abstract method definitions must be declared as protected, else typescript complains.
@@ -7,7 +7,7 @@ | |||
*/ | |||
|
|||
import { schema } from '@kbn/config-schema'; | |||
import { Layout, LogRecord, DisposableAppender } from '@kbn/logging'; | |||
import type { Layout, LogRecord, DisposableAppender } from '@kbn/logging'; |
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.
Nicely spotted!
@@ -50,10 +52,12 @@ TYPES_DEPS = [ | |||
"@npm//rxjs", | |||
"@npm//@types/moment-timezone", | |||
"@npm//elastic-apm-node", | |||
"@npm//chalk", |
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.
How did we migrate the logging service to packages without specifying @npm//chalk
in the BUILD.bazel
config? Even on main
right now, there aren't any errors. 😕 Are you sure we're not inheriting the dependency from elsewhere?
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 asked myself the same question... No idea to be honest.
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.
I left a few comments and one about an apparent 'missing' dependency regarding chalk
that's a bit puzzling.
As an MVP for browser-side logging for dev, we don't need anything more than listed in #33796 (comment).
We can handle further enhancements at a later stage.
LGTM!
@elasticmachine merge upstream |
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Public APIs missing exports
Page load bundle
Unknown metric groupsAPI count
ESLint disabled in files
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
* main: (43 commits) [Synthetics] Step details page screenshot (elastic#143452) [Lens] Datatable expression types improvement. (elastic#144173) [packages/kbn-journeys] start apm after browser start and stop after browser is closed (elastic#144267) [Files] Make files namespace agnostic (elastic#144019) Implement base browser-side logging system (elastic#144107) Correct wrong multiplier for byte conversion (elastic#143751) [Monaco] Add JSON syntax support to the Monaco editor (elastic#143739) CCS Smoke Test for Remote Clusters and Index Management (elastic#142423) [api-docs] Daily api_docs build (elastic#144294) chore(NA): include progress on Bazel tasks (elastic#144275) [RAM] Allow users to see event logs from all spaces they have access to (elastic#140449) [APM] Show recommended minimum size when going below 5 minutes (elastic#144170) [typecheck] delete temporary target_types dirs in packages (elastic#144271) [Security Solution][Endpoint] adds new alert loading utility and un-skip FTR test for endpoint (elastic#144133) [performance/journeys] revert data_stress_test_lens.ts journey step (elastic#144261) [TIP] Use search strategies in Threat Intelligence (elastic#143267) Optimize react-query dependencies (elastic#144206) [babel/node] invalidate cache when synth pkg map is updated (elastic#144258) [APM] AWS lambda estimated cost (elastic#143986) [Maps] layer group wizard (elastic#144129) ...
Summary
Part of #33796
Initial browser-side logging system implementation, as described in #33796 (comment)
Create the following packages:
@kbn/core-logging-common-internal
: extracted common parts of the logging implementation that are now reused by both the server and browser logging systems.@kbn/core-logging-browser-internal
: the browser-side logging system implementation@kbn/core-logging-browser-mocks
: the associated mocks