Skip to content

Commit 85ec4d6

Browse files
authored
feat: sync support (#5)
1 parent 31fe326 commit 85ec4d6

File tree

2 files changed

+903
-17
lines changed

2 files changed

+903
-17
lines changed

lib/cloud-logging.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ class CloudLogging {
77
this._resource = Object.assign({ type: 'global' }, options.resourceSettings)
88
this._defaultLabels = Object.assign(
99
{},
10-
{ logger: 'pino' },
10+
{ logger: 'pino', agent: 'cloud_pine' },
1111
options.defaultLabels
1212
)
1313
this._log = null
14+
// Indicates whether the logs will be forward to stdout or
15+
// through networking to GCP
16+
this._sync = options.sync ?? false
1417
this._logOptions = options.logOptions
1518
}
1619

@@ -24,14 +27,18 @@ class CloudLogging {
2427
return this._resource
2528
}
2629

30+
get sync () {
31+
return this._sync
32+
}
33+
2734
async init () {
2835
await this.logging.setProjectId()
2936
await this.logging.setDetectedResource()
3037

3138
const labels = Object.assign(
3239
{},
3340
this._resource.labels,
34-
this.logging.detectedResource.labels
41+
this.logging.detectedResource?.labels
3542
)
3643

3744
this._resource = Object.assign(
@@ -41,7 +48,10 @@ class CloudLogging {
4148

4249
this._resource.labels = labels
4350

44-
this._log = this.logging.log(this.name)
51+
this._log = this._sync
52+
? this.logging.logSync(this.name)
53+
: this.logging.log(this.name)
54+
4555
return this._log
4656
}
4757

@@ -72,6 +82,8 @@ class CloudLogging {
7282
log.meta // Custom property to add more meta to the LogEntry
7383
)
7484

85+
delete log.meta
86+
7587
meta.resource = Object.assign({}, this._resource, meta.resource)
7688
meta.labels = Object.assign({}, this._defaultLabels, meta.labels)
7789

@@ -83,8 +95,7 @@ class CloudLogging {
8395
log.message = log.message ?? log.msg
8496
delete log.msg
8597

86-
const entry = this._log.entry(meta, log)
87-
this._log.write(entry)
98+
this._log.write(this._log.entry(meta, log))
8899
}
89100
}
90101

0 commit comments

Comments
 (0)