Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 1.4.0 - tbd

### Added

- TODO

### Fixed

- Remove stack from log message, if present

## Version 1.3.1 - 2025-04-30

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions lib/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ module.exports = resource => {
attributes['exception.message'] = e.message
attributes['exception.stacktrace'] = e.stack
attributes['exception.type'] = e.name
// remove stack from message, if present
log.msg = log.msg.replace(e.stack, e.stack.split('\n')[0])
}
for (const field of custom_fields) if (field in log) attributes[field] = log[field]
logger.emit({
Expand Down
1 change: 1 addition & 0 deletions test/bookshop/srv/admin-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class AdminService extends cds.ApplicationService {
err.foo = 'bar'
cds.log('AdminService').error('Oh no!', err)
}
cds.log('AdminService').error({ message: 'Error-like oh no!', foo: 'bar' }, new Error('dummy'))
})

this.on('test_spawn', () => {
Expand Down
17 changes: 12 additions & 5 deletions test/logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,28 @@ describe('logging', () => {
const { status } = await GET('/odata/v4/admin/Genres', admin)
expect(status).to.equal(200)
const logs = console.dir.mock.calls.map(([log]) => log)
expect(logs.length).to.equal(3)
expect(logs.length).to.equal(4)
expect(logs[0]).to.include({ body: 'GET /odata/v4/admin/Genres ' }) //> why the trailing space?
expect(logs[1]).to.include({ body: 'Hello, World!' })
expect(logs[2]).to.containSubset({
body: "Oh no! Cannot read properties of undefined (reading 'exist')",
attributes: {
'log.type': 'LogRecord',
'exception.message': "Cannot read properties of undefined (reading 'exist')",
'exception.stacktrace': s =>
cds.version.split('.')[0] < 9
? s.match(/^TypeError: .+(\n\s+at .+){6}$/)
: s.match(/^TypeError: .+(\n\s+at .+){5}$/),
'exception.stacktrace': s => s.match(/^TypeError: .+(\n\s+at .+){5,}$/),
'exception.type': 'TypeError',
foo: 'bar'
}
})
expect(logs[3]).to.containSubset({
body: 'Error-like oh no! Error: dummy',
attributes: {
'log.type': 'LogRecord',
'exception.message': 'dummy',
'exception.stacktrace': s => s.match(/^Error: .+(\n\s+at .+){5,}$/),
'exception.type': 'Error',
foo: 'bar'
}
})
})
})