-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogging.mjs
More file actions
33 lines (28 loc) · 828 Bytes
/
logging.mjs
File metadata and controls
33 lines (28 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {Logging} from '@google-cloud/logging';
const logging = new Logging({projectId: "endpointservice"});
export class Logger {
constructor() {
this.backlog = [];
}
initialize(labels) {
this.labels = labels;
// Use a dedicated namespace log
this.cloudlog = logging.log(labels.namespace);
// clear backlog
this.backlog.forEach(msg => this.log(msg))
this.backlog = undefined;
}
log(msg) {
if (this.cloudlog === undefined) {
this.backlog.push(msg);
} else {
const entry = this.cloudlog.entry({
resource: {
type: 'generic_task',
labels: this.labels,
}
}, msg);
return this.cloudlog.write(entry);
}
}
}