Skip to content

Commit

Permalink
Merge pull request #84 from JaniAnttonen/development
Browse files Browse the repository at this point in the history
New release with the fix to [object Object]
  • Loading branch information
JaniAnttonen authored Nov 12, 2021
2 parents 65f1a43 + 0d1a702 commit e36722b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LokiTransport extends Transport {
}`

// Make sure all label values are strings
lokiLabels = JSON.parse(JSON.stringify(lokiLabels, (key, value) => value ? value.toString() : value))
lokiLabels = Object.fromEntries(Object.entries(lokiLabels).map(([key, value]) => [key, value ? value.toString() : value]))

// Construct the log to fit Grafana Loki's accepted format
const logEntry = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-loki",
"version": "6.0.2",
"version": "6.0.3",
"description": "A Winston transport for Grafana Loki",
"keywords": [
"winston",
Expand Down
9 changes: 3 additions & 6 deletions test/custom-labels-lines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ describe('Integration tests', function () {

const testMessage = 'testMessage'
const testLabel = 'testLabel'
const now = Date.now() / 1000
const now = Date.now()
logger.debug({ message: testMessage, labels: { customLabel: testLabel } })
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(
lokiTransport.batcher.batch.streams[0]
).toEqual({
labels: `{level="debug",module="name",app="appname",customLabel="${testLabel}"}`,
labels: { level: 'debug', module: 'name', app: 'appname', customLabel: testLabel },
entries: [{
line: `[name] ${testMessage}`,
timestamp: {
nanos: expect.any(Number),
seconds: expect.toBeWithinRange(now - 5, now + 5)
}
ts: expect.toBeWithinRange(now - 5, now + 5)
}]
})
})
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
}
],
"logs_mapped_before": [
"{\"labels\":\"{job=\\\"test\\\", level=\\\"info\\\"}\",\"entries\":[{\"ts\":1546977515828,\"line\":\"testings \"}]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything \"} ]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything but not quite \"}]}",
"{\"labels\":\"{job=\\\"test\\\", level=\\\"error\\\", jeejee=\\\"ebon\\\"}\",\"entries\":[{\"ts\":1546977515858,\"line\":\"you broke everything but not quite \"]}"],
"{\"labels\":{\"job\":\"test\", \"level\":\"info\"},\"entries\":[{\"ts\":1546977515828,\"line\":\"testings \"}]}",
"{\"labels\":\"{\"job\":\"test\", \"level\":\"error\"}\",\"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything \"} ]}",
"{\"labels\":{\"level\":\"error\",\"job\":\"test\"}, \"entries\":[{\"ts\":1546977615848,\"line\":\"you broke everything but not quite \"}]}",
"{\"labels\":{\"level\":\"error\", \"jeejee\":\"ebon\", \"job\":\"test\"},\"entries\":[{\"ts\":1546977515858,\"line\":\"you broke everything but not quite \"}]}"],
"logs_mapped_after": [
"{\"stream\":\"{job=\\\"test\\\", level=\\\"info\\\"}\",\"values\":[1546977515828,\"testings \"]}",
"{\"stream\":\"{job=\\\"test\\\", level=\\\"error\\\"}\",\"values\":[1546977615848,\"you broke everything \"]}",
Expand Down
11 changes: 10 additions & 1 deletion test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ describe('Integration tests', function () {
lokiTransport.log(fixtures.logs[0], () => {})
expect(lokiTransport.batcher.batch.streams.length).toBe(1)
expect(lokiTransport.batcher.batch.streams[0]).toEqual(
fixtures.logs[0]
{
entries: [{
line: 'testings ',
ts: 1546977515828
}],
'labels': {
job: 'test',
level: 'info'
}
}
)
})
it("LokiTransport should append anything else than the message after it in the log's entry", function () {
Expand Down

0 comments on commit e36722b

Please sign in to comment.