Skip to content

Commit 84711ee

Browse files
authored
feat: initial commit (#1)
1 parent c82359f commit 84711ee

File tree

9 files changed

+554
-16
lines changed

9 files changed

+554
-16
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
node: [14.x, 16.x, 17.x]
9+
node: [14.x, 16.x, 18.x]
1010
name: Node ${{ matrix.node }}
1111
steps:
1212
- uses: actions/checkout@v1
@@ -15,6 +15,4 @@ jobs:
1515
with:
1616
node-version: ${{ matrix.node }}
1717
- run: npm install
18-
- run: npm run lint:ci
1918
- run: npm run test:ci
20-
- run: npm run typescript

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
# As is a package, the lockfile is not needed
107+
package-lock.json

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# lib_template
2-
Template repository for bootstrap new libraries faster
1+
# Cloud Pine
2+
Pino Transport abstraction for Google Cloud Logging.

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
'use strict'
2+
const split = require('split2')
3+
const { CloudLogging } = require('./lib/cloud-logging')
4+
5+
async function CloudPine (options) {
6+
const { logName = 'Cloud_Pine', cloudLoggingOptions } = options
7+
const logging = new CloudLogging(logName, cloudLoggingOptions)
8+
9+
await logging.init()
10+
11+
const stream = split(logging.parseLine.bind(logging), { autoDestroy: true })
12+
13+
return stream
14+
}
15+
16+
module.exports = CloudPine

lib/cloud-logging.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const { Logging } = require('@google-cloud/logging')
2+
3+
class CloudLogging {
4+
constructor (logName, options = {}) {
5+
this.name = logName
6+
this.logging = new Logging(options.googleCloudOptions)
7+
this._resource = Object.assign({ type: 'global' }, options.resourceSettings)
8+
this._defaultLabels = Object.assign(
9+
{},
10+
{ logger: 'pino' },
11+
options.defaultLabels
12+
)
13+
this._log = null
14+
this._logOptions = options.logOptions
15+
}
16+
17+
mapSeverity (logLevel) {
18+
return (
19+
CloudLogging.SEVERITY_MAP[logLevel] || CloudLogging.SEVERITY_MAP['30']
20+
)
21+
}
22+
23+
get resource () {
24+
return this._resource
25+
}
26+
27+
async init () {
28+
await this.logging.setProjectId()
29+
await this.logging.setDetectedResource()
30+
31+
const labels = Object.assign(
32+
{},
33+
this._resource.labels,
34+
this.logging.detectedResource.labels
35+
)
36+
37+
this._resource = Object.assign(
38+
this._resource,
39+
this.logging.detectedResource
40+
)
41+
42+
this._resource.labels = labels
43+
44+
this._log = this.logging.log(this.name)
45+
return this._log
46+
}
47+
48+
parseLine (line) {
49+
let log
50+
51+
try {
52+
log = JSON.parse(line)
53+
} catch (error) {
54+
const meta = {
55+
severity: CloudLogging.SEVERITY_MAP['50'],
56+
resource: this._resource,
57+
labels: this._defaultLabels
58+
}
59+
const entry = this._log.entry(meta, {
60+
error,
61+
message: 'Malformed log entry'
62+
})
63+
64+
this._log.write(entry)
65+
66+
// We do nothing else as the log was malformed
67+
return
68+
}
69+
70+
const meta = Object.assign(
71+
{ severity: this.mapSeverity(log.level) },
72+
log.meta // Custom property to add more meta to the LogEntry
73+
)
74+
75+
meta.resource = Object.assign({}, this._resource, meta.resource)
76+
meta.labels = Object.assign({}, this._defaultLabels, meta.labels)
77+
78+
if (log.httpRequest) {
79+
meta.httpRequest = log.httpRequest
80+
delete log.httpRequest
81+
}
82+
83+
log.message = log.message ?? log.msg
84+
delete log.msg
85+
86+
const entry = this._log.entry(meta, log)
87+
this._log.write(entry)
88+
}
89+
}
90+
91+
CloudLogging.SEVERITY_MAP = {
92+
10: 'DEBUG',
93+
20: 'DEBUG',
94+
30: 'INFO',
95+
40: 'WARNING',
96+
50: 'ERROR',
97+
60: 'CRITICAL'
98+
}
99+
100+
module.exports = {
101+
CloudLogging
102+
}

lib/index.js

Whitespace-only changes.

package.json

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,55 @@
11
{
2-
"name": "<lib_name>",
2+
"name": "cloud-pine",
33
"version": "0.0.0",
4-
"description": "<description>",
4+
"description": "Pino Transport abstraction for Google Cloud Logging.",
55
"main": "index.js",
6+
"bin": {
7+
"cloud-pine": "./cli.js"
8+
},
69
"types": "index.d.ts",
710
"scripts": {
8-
"test": "tap --cov test/*.test.js && npm run typescript",
9-
"test:ci": "tap --cov test/*.test.js && npm run typescript && npm run lint",
11+
"test": "tap --cov test/**/*.test.js && npm run test:ts && npm run lint",
12+
"test:ci": "tap --cov test/**/*.test.js && npm run test:ts && npm run lint:ci",
1013
"test:only": "tap --only",
1114
"test:unit": "tap test/*.test.js",
15+
"test:ts": "tsd",
1216
"lint": "standard | snazzy",
13-
"lint:ci": "standard",
14-
"typescript": "tsd"
17+
"lint:ci": "standard"
1518
},
16-
"keywords": [],
19+
"keywords": [
20+
"pino",
21+
"pine",
22+
"gcp",
23+
"google-cloud",
24+
"google-cloud-platform",
25+
"cloud",
26+
"cloud-pine",
27+
"logging",
28+
"cloud-logging"
29+
],
1730
"repository": {
1831
"type": "git",
19-
"url": "git+https://github.com/metcoder95/<lib_name>.git"
32+
"url": "git+https://github.com/metcoder95/cloud-pine.git"
2033
},
21-
"readme": "https://github.com/metcoder95/<lib_name>/blob/main/README.md",
34+
"readme": "https://github.com/metcoder95/cloud-pine/blob/main/README.md",
2235
"bugs": {
23-
"url": "https://github.com/metcoder95/<lib_name>/issues"
36+
"url": "https://github.com/metcoder95/cloud-pine/issues"
2437
},
2538
"author": "metcoder95 <[email protected]>",
2639
"license": "MIT",
2740
"devDependencies": {
2841
"@types/node": "^14.17.6",
2942
"husky": "^7.0.2",
43+
"pino": "^8.2.0",
3044
"snazzy": "^9.0.0",
3145
"standard": "^16.0.3",
3246
"tap": "^15.0.10",
3347
"tsd": "^0.17.0",
3448
"typescript": "^4.4"
3549
},
3650
"dependencies": {
51+
"@google-cloud/logging": "^10.1.1",
52+
"split2": "^4.1.0"
3753
},
3854
"tsd": {
3955
"directory": "test"

test/index.test.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)