Skip to content

Commit aa8246f

Browse files
committedFeb 3, 2025
add betterstack logging support to log configuration
1 parent 3db6fa3 commit aa8246f

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
 

‎src/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ interface LogConfigOptions {
1818
token?: string
1919
level?: Levels
2020
}
21+
betterstack?: {
22+
disabled?: boolean
23+
onlyInProd?: boolean
24+
host?: string
25+
token?: string
26+
level?: Levels
27+
}
2128
teams?: {
2229
disabled?: boolean
2330
onlyInProd?: boolean

‎src/lib/log-config-factory.js

+41
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ function _logConfigFactory (options = {}, { axios, deepmerge, loggerOptions, env
1212
url: envVariables.TEAMS_WEBHOOK_URL
1313
}
1414
}
15+
if ((!options || !options.betterstack) && envVariables.BETTERSTACK_URL && envVariables.BETTERSTACK_TOKEN) {
16+
options.betterstack = {
17+
url: envVariables.BETTERSTACK_URL,
18+
token: envVariables.BETTERSTACK_TOKEN
19+
}
20+
}
1521
if (options && typeof options === 'object') {
1622
// remote logging
1723
if (options.remote && typeof options.remote === 'object') {
@@ -36,6 +42,41 @@ function _logConfigFactory (options = {}, { axios, deepmerge, loggerOptions, env
3642
loggerOptions.remoteLevel = options.remote.level
3743
}
3844
}
45+
46+
// Betterstack logging
47+
if (options.betterstack && typeof options.betterstack === 'object') {
48+
options.betterstack.url = options.betterstack.url || envVariables.BETTERSTACK_URL
49+
options.betterstack.token = options.betterstack.token || envVariables.BETTERSTACK_TOKEN
50+
51+
loggerOptions.betterstackLogger = {
52+
log: async msg => {
53+
console.log('vi skal logge til betterstack')
54+
console.log(options.betterstack.url)
55+
const betterstackUrl = new URL(options.betterstack.url)
56+
if (!betterstackUrl.hostname.endsWith('betterstackdata.com')) {
57+
throw new Error('Invalid Betterstack URL, must end with betterstackdata.com')
58+
}
59+
if (!betterstackUrl.protocol === 'https:') {
60+
throw new Error('Invalid Betterstack URL, must use HTTPS')
61+
}
62+
await axios.post(options.betterstack.url, msg, { headers: { Authorization: `Bearer ${options.betterstack.token}` } })
63+
}
64+
}
65+
66+
// onlyInProd defaults to true
67+
loggerOptions.onlyInProd = options.betterstack.onlyInProd === undefined ? true : options.betterstack.onlyInProd
68+
options.betterstack.onlyInProd = loggerOptions.onlyInProd
69+
70+
// enables betterstack logging if everything checks out, otherwise betterstack logging will be disabled
71+
loggerOptions.logToBetterstack = !options.betterstack.disabled &&
72+
typeof options.betterstack.url === 'string' &&
73+
typeof options.betterstack.token === 'string'
74+
75+
// set betterstack level (lowest level for betterstack logging)
76+
if (options.betterstack.level) {
77+
loggerOptions.betterstackLevel = options.betterstack.level
78+
}
79+
}
3980
// Teams logging
4081
if (options.teams && typeof options.teams === 'object') {
4182
options.teams.url = options.teams.url || envVariables.TEAMS_WEBHOOK_URL

‎src/lib/logger-factory.js

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ async function _loggerFactory (level, message, { formatDateTime, logLevelMapper,
3434
const messageFormats = formatLogMessage(formatDateTime, pkg, logLevel, messageArray, context)
3535
const remoteLevel = (loggerOptions.remoteLevel && logLevelMapper(loggerOptions.remoteLevel)) || undefined
3636
const remoteLevelLogToRemote = remoteLevel ? logLevel.severity <= remoteLevel.severity : true
37+
const betterstackLevel = (loggerOptions.betterstackLevel && logLevelMapper(loggerOptions.betterstackLevel)) || undefined
38+
const betterstackLevelLogToBetterstack = betterstackLevel ? logLevel.severity <= betterstackLevel.severity : true
3739
const teamsLevel = (loggerOptions.teamsLevel && logLevelMapper(loggerOptions.teamsLevel)) || undefined
3840
const remoteLevelLogToTeams = teamsLevel ? logLevel.severity <= teamsLevel.severity : true
3941

@@ -50,6 +52,16 @@ async function _loggerFactory (level, message, { formatDateTime, logLevelMapper,
5052
localLog(loggerOptions, warnLevel, errorMessage)
5153
}
5254

55+
// Betterstack logging
56+
const shouldLogToBetterstack = (loggerOptions.logToBetterstack && !(!inProduction && loggerOptions.onlyInProd) && betterstackLevelLogToBetterstack) || false
57+
try {
58+
if (shouldLogToBetterstack) await loggerOptions.betterstackLogger.log(messageFormats.betterstackLogMessage)
59+
} catch (error) {
60+
const warnLevel = logLevelMapper('warn')
61+
const errorMessage = formatLogMessage(formatDateTime, pkg, warnLevel, ['logger-factory', 'logToBetterstack', 'error', error.message])
62+
localLog(loggerOptions, warnLevel, errorMessage)
63+
}
64+
5365
// Teams logging
5466
const shouldLogToTeams = (loggerOptions.logToTeams && !(!inProduction && loggerOptions.onlyTeamsInProd) && remoteLevelLogToTeams) || false
5567
try {
@@ -89,6 +101,7 @@ function formatLogMessage (formatDateTime, pkg, logLevel, messageArray, context)
89101
return {
90102
logMessage,
91103
remoteLogMessage: `${logLevel.level} - ${logMessage}`,
104+
betterstackLogMessage: `${logLevel.level} - ${logMessage}`,
92105
localLogMessage: `[ ${fDate} ${fTime} ] < ${logLevel.level} >${logLevel.padding} ${logMessage}`,
93106
teamsMessageCard: formatMessageCard(logLevel, `${logLevel.level} - ${funcDetails}`, messageArray),
94107
teamsAdaptiveCard: formatAdaptiveCard(logLevel, `${logLevel.level} - ${funcDetails}`, messageArray)

0 commit comments

Comments
 (0)