Skip to content

Commit 42fbab0

Browse files
committed
Tag all log messages
1 parent d2ff868 commit 42fbab0

File tree

22 files changed

+301
-78
lines changed

22 files changed

+301
-78
lines changed

glean/src/cli.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import * as path from "path";
1010
import { argv, platform } from "process";
1111
import { promisify } from "util";
1212

13+
import log, { LoggingLevel } from "./core/log.js";
14+
15+
const LOG_TAG = "CLI";
16+
1317
// The name of the directory which will contain the Python virtual environment
1418
// used to run the glean-parser.
1519
const VIRTUAL_ENVIRONMENT_DIR = ".venv";
@@ -100,7 +104,7 @@ function getPythonVenvBinariesPath(venvRoot: string): string {
100104
* is accessible, `false` otherwise.
101105
*/
102106
async function checkPythonVenvExists(venvPath: string): Promise<boolean> {
103-
console.log(`Checking for a Glean virtual environment at ${venvPath}`);
107+
log(LOG_TAG, `Checking for a Glean virtual environment at ${venvPath}`);
104108

105109
const venvPython =
106110
path.join(getPythonVenvBinariesPath(venvPath), getSystemPythonBinName());
@@ -124,7 +128,7 @@ async function checkPythonVenvExists(venvPath: string): Promise<boolean> {
124128
* @returns `true` if the environment was correctly created, `false` otherwise.
125129
*/
126130
async function createPythonVenv(venvPath: string): Promise<boolean> {
127-
console.log(`Creating a Glean virtual environment at ${venvPath}`);
131+
log(LOG_TAG, `Creating a Glean virtual environment at ${venvPath}`);
128132

129133
const pipFilename = (platform === "win32") ? "pip3.exe" : "pip3";
130134
const venvPip =
@@ -140,10 +144,10 @@ async function createPythonVenv(venvPath: string): Promise<boolean> {
140144
});
141145

142146
stopSpinner(spinner);
143-
console.log(`${stdout}`);
147+
log(LOG_TAG, `${stdout}`);
144148

145149
if (err) {
146-
console.error(`${stderr}`);
150+
log(LOG_TAG, `${stderr}`);
147151
return false;
148152
}
149153
}
@@ -162,9 +166,9 @@ async function setup(projectRoot: string) {
162166

163167
const venvExists = await checkPythonVenvExists(venvRoot);
164168
if (venvExists) {
165-
console.log(`Using Glean virtual environment at ${venvRoot}`);
169+
log(LOG_TAG, `Using Glean virtual environment at ${venvRoot}`);
166170
} else if (!await createPythonVenv(venvRoot)){
167-
console.error(`Failed to create a Glean virtual environment at ${venvRoot}`);
171+
log(LOG_TAG, `Failed to create a Glean virtual environment at ${venvRoot}`);
168172
process.exit(1);
169173
}
170174
}
@@ -186,10 +190,10 @@ async function runGlean(projectRoot: string, parserArgs: string[]) {
186190
});
187191
188192
stopSpinner(spinner);
189-
console.log(`${stdout}`);
193+
log(LOG_TAG, `${stdout}`);
190194
191195
if (err) {
192-
console.error(`${stderr}`);
196+
log(LOG_TAG, `${stderr}`);
193197
process.exit(1);
194198
}
195199
}
@@ -232,7 +236,7 @@ async function run(args: string[]) {
232236
try {
233237
await setup(projectRoot);
234238
} catch (err) {
235-
console.error("Failed to setup the Glean build environment", err);
239+
log(LOG_TAG, ["Failed to setup the Glean build environment.\n", err], LoggingLevel.Error);
236240
process.exit(1);
237241
}
238242

@@ -241,6 +245,6 @@ async function run(args: string[]) {
241245

242246
// For discoverability, try to leave this function as the last one on this file.
243247
run(argv).catch(e => {
244-
console.error("There was an error running Glean", e);
248+
log(LOG_TAG, ["There was an error running Glean.\n", e], LoggingLevel.Error);
245249
process.exit(1);
246250
});

glean/src/core/config.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import type Plugin from "../plugins/index.js";
77
import { validateHeader, validateURL } from "./utils.js";
88
import type Uploader from "./upload/uploader.js";
99
import type { DebugOptions } from "./debug_options.js";
10+
import log, { LoggingLevel } from "./log.js";
11+
12+
const LOG_TAG = "core.Config";
1013

1114
/**
1215
* Describes how to configure Glean.
@@ -69,23 +72,35 @@ export class Configuration implements ConfigurationInterface {
6972
static validateDebugViewTag(tag: string): boolean {
7073
const validation = validateHeader(tag);
7174
if (!validation) {
72-
console.error(
73-
`"${tag}" is not a valid \`debugViewTag\` value.`,
74-
"Please make sure the value passed satisfies the regex `^[a-zA-Z0-9-]{1,20}$`."
75+
log(
76+
LOG_TAG,
77+
[
78+
`"${tag}" is not a valid \`debugViewTag\` value.`,
79+
"Please make sure the value passed satisfies the regex `^[a-zA-Z0-9-]{1,20}$`."
80+
],
81+
LoggingLevel.Error
7582
);
7683
}
7784
return validation;
7885
}
7986

8087
static validateSourceTags(tags: string[]): boolean {
8188
if (tags.length < 1 || tags.length > GLEAN_MAX_SOURCE_TAGS) {
82-
console.error(`A list of tags cannot contain more than ${GLEAN_MAX_SOURCE_TAGS} elements.`);
89+
log(
90+
LOG_TAG,
91+
`A list of tags cannot contain more than ${GLEAN_MAX_SOURCE_TAGS} elements.`,
92+
LoggingLevel.Error
93+
);
8394
return false;
8495
}
8596

8697
for (const tag of tags) {
8798
if (tag.startsWith("glean")) {
88-
console.error("Tags starting with `glean` are reserved and must not be used.");
99+
log(
100+
LOG_TAG,
101+
"Tags starting with `glean` are reserved and must not be used.",
102+
LoggingLevel.Error
103+
);
89104
return false;
90105
}
91106

glean/src/core/dispatcher.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
import log, { LoggingLevel } from "./log.js";
6+
7+
const LOG_TAG = "core.Dispatcher";
8+
59
// The possible states a dispatcher instance can be in.
610
export const enum DispatcherState {
711
// The dispatcher has not been initialized yet.
@@ -84,7 +88,7 @@ class Dispatcher {
8488
try {
8589
await task();
8690
} catch(e) {
87-
console.error("Error executing task:", e);
91+
log(LOG_TAG, ["Error executing task:", e], LoggingLevel.Error);
8892
}
8993
}
9094

@@ -144,9 +148,13 @@ class Dispatcher {
144148
}
145149
})
146150
.catch(error => {
147-
console.error(
148-
"IMPOSSIBLE: Something went wrong while the dispatcher was executing the tasks queue.",
149-
error
151+
log(
152+
LOG_TAG,
153+
[
154+
"IMPOSSIBLE: Something went wrong while the dispatcher was executing the tasks queue.",
155+
error
156+
],
157+
LoggingLevel.Error
150158
);
151159
});
152160
}
@@ -169,7 +177,11 @@ class Dispatcher {
169177
private launchInternal(command: Command, priorityTask = false): boolean {
170178
if (!priorityTask && this.state === DispatcherState.Uninitialized) {
171179
if (this.queue.length >= this.maxPreInitQueueSize) {
172-
console.warn("Unable to enqueue task, pre init queue is full.");
180+
log(
181+
LOG_TAG,
182+
"Unable to enqueue task, pre init queue is full.",
183+
LoggingLevel.Warn
184+
);
173185
return false;
174186
}
175187
}
@@ -213,7 +225,11 @@ class Dispatcher {
213225
*/
214226
flushInit(task?: Task): void {
215227
if (this.state !== DispatcherState.Uninitialized) {
216-
console.warn("Attempted to initialize the Dispatcher, but it is already initialized. Ignoring.");
228+
log(
229+
LOG_TAG,
230+
"Attempted to initialize the Dispatcher, but it is already initialized. Ignoring.",
231+
LoggingLevel.Warn
232+
);
217233
return;
218234
}
219235

glean/src/core/error/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import type { MetricType } from "../metrics/index.js";
66
import type { ErrorType } from "./error_type.js";
77
import CounterMetricType from "../metrics/types/counter.js";
88
import { combineIdentifierAndLabel, stripLabel } from "../metrics/types/labeled.js";
9+
import log from "../log.js";
10+
11+
/**
12+
* Create a log tag for a specific metric type.
13+
*
14+
* @param metric The metric type to create a tag for.
15+
* @returns The tag.
16+
*/
17+
function createLogTag(metric: MetricType): string {
18+
const capitalizedType = metric.type.charAt(0).toUpperCase() + metric.type.slice(1);
19+
return `core.Metrics.${capitalizedType}`;
20+
}
921

1022
/**
1123
* For a given metric, get the metric in which to record errors.
@@ -54,7 +66,7 @@ export default class ErrorManager {
5466
numErrors = 1
5567
): Promise<void> {
5668
const errorMetric = getErrorMetricForMetric(metric, error);
57-
console.warn(`${metric.baseIdentifier()}: ${message}`);
69+
log(createLogTag(metric), `${metric.baseIdentifier()}: ${message}`);
5870
if (numErrors > 0) {
5971
await CounterMetricType._private_addUndispatched(errorMetric, numErrors);
6072
} else {

glean/src/core/events/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import type Plugin from "../../plugins/index.js";
6+
import log, { LoggingLevel } from "../log.js";
67

78
import type { PingPayload } from "../pings/ping_payload.js";
89
import type { JSONObject } from "../utils.js";
910

11+
const LOG_TAG = "core.Events";
12+
1013
export class CoreEvent<
1114
// An array of arguments that the event will provide as context to the plugin action.
1215
Context extends unknown[] = unknown[],
@@ -29,10 +32,14 @@ export class CoreEvent<
2932
*/
3033
registerPlugin(plugin: Plugin<CoreEvent<Context, Result>>): void {
3134
if (this.plugin) {
32-
console.error(
33-
`Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`,
34-
`That event is already watched by plugin '${this.plugin.name}'`,
35-
`Plugin '${plugin.name}' will be ignored.`
35+
log(
36+
LOG_TAG,
37+
[
38+
`Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`,
39+
`That event is already watched by plugin '${this.plugin.name}'`,
40+
`Plugin '${plugin.name}' will be ignored.`
41+
],
42+
LoggingLevel.Error
3643
);
3744
return;
3845
}

glean/src/core/events/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import type { CoreEvent } from "./index.js";
66
import CoreEvents from "./index.js";
77
import type Plugin from "../../plugins/index.js";
8+
import log, { LoggingLevel } from "../log.js";
9+
10+
const LOG_TAG = "core.Events.Utils";
811

912
/**
1013
* Registers a plugin to the desired Glean event.
@@ -21,9 +24,13 @@ export function registerPluginToEvent<E extends CoreEvent>(plugin: Plugin<E>): v
2124
return;
2225
}
2326

24-
console.error(
25-
`Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`,
26-
"That is not a valid Glean event. Ignoring"
27+
log(
28+
LOG_TAG,
29+
[
30+
`Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`,
31+
"That is not a valid Glean event. Ignoring"
32+
],
33+
LoggingLevel.Error
2734
);
2835
}
2936

glean/src/core/glean.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import TestPlatform from "../platform/test/index.js";
2121
import { Lifetime } from "./metrics/lifetime.js";
2222
import { Context } from "./context.js";
2323
import PingType from "./pings/ping_type.js";
24+
import log, { LoggingLevel } from "./log.js";
25+
26+
const LOG_TAG = "core.Glean";
2427

2528
class Glean {
2629
// The Glean singleton.
@@ -179,17 +182,29 @@ class Glean {
179182
config?: ConfigurationInterface
180183
): void {
181184
if (Context.initialized) {
182-
console.warn("Attempted to initialize Glean, but it has already been initialized. Ignoring.");
185+
log(
186+
LOG_TAG,
187+
"Attempted to initialize Glean, but it has already been initialized. Ignoring.",
188+
LoggingLevel.Warn
189+
);
183190
return;
184191
}
185192

186193
if (applicationId.length === 0) {
187-
console.error("Unable to initialize Glean, applicationId cannot be an empty string.");
194+
log(
195+
LOG_TAG,
196+
"Unable to initialize Glean, applicationId cannot be an empty string.",
197+
LoggingLevel.Error
198+
);
188199
return;
189200
}
190201

191202
if (!Glean.instance._platform) {
192-
console.error("Unable to initialize Glean, environment has not been set.");
203+
log(
204+
LOG_TAG,
205+
"Unable to initialize Glean, environment has not been set.",
206+
LoggingLevel.Error
207+
);
193208
return;
194209
}
195210

@@ -316,10 +331,14 @@ class Glean {
316331
static setUploadEnabled(flag: boolean): void {
317332
Context.dispatcher.launch(async () => {
318333
if (!Context.initialized) {
319-
console.error(
320-
"Changing upload enabled before Glean is initialized is not supported.\n",
321-
"Pass the correct state into `Glean.initialize\n`.",
322-
"See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk`"
334+
log(
335+
LOG_TAG,
336+
[
337+
"Changing upload enabled before Glean is initialized is not supported.\n",
338+
"Pass the correct state into `Glean.initialize\n`.",
339+
"See documentation at https://mozilla.github.io/glean/book/user/general-api.html#initializing-the-glean-sdk`"
340+
],
341+
LoggingLevel.Error
323342
);
324343
return;
325344
}
@@ -362,7 +381,7 @@ class Glean {
362381
*/
363382
static setDebugViewTag(value: string): void {
364383
if (!Configuration.validateDebugViewTag(value)) {
365-
console.error(`Invalid \`debugViewTag\` ${value}. Ignoring.`);
384+
log(LOG_TAG, `Invalid \`debugViewTag\` ${value}. Ignoring.`, LoggingLevel.Error);
366385
return;
367386
}
368387

@@ -388,7 +407,7 @@ class Glean {
388407
*/
389408
static setSourceTags(value: string[]): void {
390409
if (!Configuration.validateSourceTags(value)) {
391-
console.error(`Invalid \`sourceTags\` ${value.toString()}. Ignoring.`);
410+
log(LOG_TAG, `Invalid \`sourceTags\` ${value.toString()}. Ignoring.`, LoggingLevel.Error);
392411
return;
393412
}
394413

@@ -426,7 +445,8 @@ class Glean {
426445
// we log a debug message about the change.
427446
if (Glean.instance._platform && Glean.instance._platform.name !== platform.name) {
428447
// TODO: Only show this message outside of test mode, and rephrase it as an error (depends on Bug 1682771).
429-
console.debug(
448+
log(
449+
LOG_TAG,
430450
`Changing Glean platform from "${Glean.platform.name}" to "${platform.name}".`,
431451
);
432452
}

glean/src/core/internal_metrics.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import type { ConfigurationInterface } from "./config.js";
1313
import type Platform from "../platform/index.js";
1414
import type MetricsDatabase from "./metrics/database.js";
1515
import { Lifetime } from "./metrics/lifetime.js";
16+
import log, { LoggingLevel } from "./log.js";
17+
18+
const LOG_TAG = "core.InternalMetrics";
1619

1720
/**
1821
* Glean internal metrics.
@@ -124,7 +127,7 @@ export class CoreMetrics {
124127
needNewClientId = true;
125128
}
126129
} catch {
127-
console.warn("Unexpected value found for Glean clientId. Ignoring.");
130+
log(LOG_TAG, "Unexpected value found for Glean clientId. Ignoring.", LoggingLevel.Warn);
128131
needNewClientId = true;
129132
}
130133
} else {

0 commit comments

Comments
 (0)