@@ -12,6 +12,12 @@ function _logConfigFactory (options = {}, { axios, deepmerge, loggerOptions, env
12
12
url : envVariables . TEAMS_WEBHOOK_URL
13
13
}
14
14
}
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
+ }
15
21
if ( options && typeof options === 'object' ) {
16
22
// remote logging
17
23
if ( options . remote && typeof options . remote === 'object' ) {
@@ -36,6 +42,41 @@ function _logConfigFactory (options = {}, { axios, deepmerge, loggerOptions, env
36
42
loggerOptions . remoteLevel = options . remote . level
37
43
}
38
44
}
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
+ }
39
80
// Teams logging
40
81
if ( options . teams && typeof options . teams === 'object' ) {
41
82
options . teams . url = options . teams . url || envVariables . TEAMS_WEBHOOK_URL
0 commit comments