|
| 1 | +/* eslint-disable */ |
| 2 | +// @ts-nocheck |
| 3 | + |
| 4 | +/** |
| 5 | + * Internal fork of the unmaintained strong-log-transformer package. |
| 6 | + * https://github.com/strongloop/strong-log-transformer/blob/3315d59bc4c912d025e15a6ca22a600a85406f14/lib/logger.js |
| 7 | + * |
| 8 | + * Notable changes: |
| 9 | + * - replaced the dependency on duplexer, which has been deprecated for a long time, with inline createDuplex() |
| 10 | + * - modernized core node.js imports and removed the usage of deprecated node:util._extend() |
| 11 | + */ |
| 12 | + |
| 13 | +// Copyright IBM Corp. 2014,2018. All Rights Reserved. |
| 14 | +// Node module: strong-log-transformer |
| 15 | +// This file is licensed under the Apache License 2.0. |
| 16 | +// License text available at https://opensource.org/licenses/Apache-2.0 |
| 17 | + |
| 18 | +import stream from "node:stream"; |
| 19 | +import { StringDecoder } from "node:string_decoder"; |
| 20 | +import util from "node:util"; |
| 21 | +import through from "through"; |
| 22 | + |
| 23 | +export default Logger; |
| 24 | + |
| 25 | +Logger.DEFAULTS = { |
| 26 | + format: "text", |
| 27 | + tag: "", |
| 28 | + mergeMultiline: false, |
| 29 | + timeStamp: false, |
| 30 | +}; |
| 31 | + |
| 32 | +var formatters = { |
| 33 | + text: textFormatter, |
| 34 | + json: jsonFormatter, |
| 35 | +}; |
| 36 | + |
| 37 | +function Logger(options?) { |
| 38 | + var defaults = JSON.parse(JSON.stringify(Logger.DEFAULTS)); |
| 39 | + options = Object.assign(defaults, options || {}); |
| 40 | + var catcher = deLiner(); |
| 41 | + var emitter = catcher; |
| 42 | + var transforms = [objectifier()]; |
| 43 | + |
| 44 | + if (options.tag) { |
| 45 | + transforms.push(staticTagger(options.tag)); |
| 46 | + } |
| 47 | + |
| 48 | + if (options.mergeMultiline) { |
| 49 | + transforms.push(lineMerger()); |
| 50 | + } |
| 51 | + |
| 52 | + // TODO |
| 53 | + // if (options.pidStamp) { |
| 54 | + // transforms.push(pidStamper(options.pid)); |
| 55 | + // } |
| 56 | + |
| 57 | + // TODO |
| 58 | + // if (options.workerStamp) { |
| 59 | + // transforms.push(workerStamper(options.worker)); |
| 60 | + // } |
| 61 | + |
| 62 | + transforms.push(formatters[options.format](options)); |
| 63 | + |
| 64 | + // restore line endings that were removed by line splitting |
| 65 | + transforms.push(reLiner()); |
| 66 | + |
| 67 | + for (var t in transforms) { |
| 68 | + emitter = emitter.pipe(transforms[t]); |
| 69 | + } |
| 70 | + |
| 71 | + return createDuplex(catcher, emitter); |
| 72 | +} |
| 73 | + |
| 74 | +function deLiner() { |
| 75 | + var decoder = new StringDecoder("utf8"); |
| 76 | + var last = ""; |
| 77 | + |
| 78 | + return new stream.Transform({ |
| 79 | + transform(chunk, _enc, callback) { |
| 80 | + last += decoder.write(chunk); |
| 81 | + var list = last.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g); |
| 82 | + last = list.pop(); |
| 83 | + for (var i = 0; i < list.length; i++) { |
| 84 | + // swallow empty lines |
| 85 | + if (list[i]) { |
| 86 | + this.push(list[i]); |
| 87 | + } |
| 88 | + } |
| 89 | + callback(); |
| 90 | + }, |
| 91 | + flush(callback) { |
| 92 | + // incomplete UTF8 sequences become UTF8 replacement characters |
| 93 | + last += decoder.end(); |
| 94 | + if (last) { |
| 95 | + this.push(last); |
| 96 | + } |
| 97 | + callback(); |
| 98 | + }, |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +function reLiner() { |
| 103 | + return through(appendNewline); |
| 104 | + |
| 105 | + function appendNewline(line) { |
| 106 | + this.emit("data", line + "\n"); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +function objectifier() { |
| 111 | + return through(objectify, null, { autoDestroy: false }); |
| 112 | + |
| 113 | + function objectify(line) { |
| 114 | + this.emit("data", { |
| 115 | + msg: line, |
| 116 | + time: Date.now(), |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function staticTagger(tag) { |
| 122 | + return through(tagger); |
| 123 | + |
| 124 | + function tagger(logEvent) { |
| 125 | + logEvent.tag = tag; |
| 126 | + this.emit("data", logEvent); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function textFormatter(options) { |
| 131 | + return through(textify); |
| 132 | + |
| 133 | + function textify(logEvent) { |
| 134 | + var line = util.format("%s%s", textifyTags(logEvent.tag), logEvent.msg.toString()); |
| 135 | + if (options.timeStamp) { |
| 136 | + line = util.format("%s %s", new Date(logEvent.time).toISOString(), line); |
| 137 | + } |
| 138 | + this.emit("data", line.replace(/\n/g, "\\n")); |
| 139 | + } |
| 140 | + |
| 141 | + function textifyTags(tags) { |
| 142 | + var str = ""; |
| 143 | + if (typeof tags === "string") { |
| 144 | + str = tags + " "; |
| 145 | + } else if (typeof tags === "object") { |
| 146 | + for (var t in tags) { |
| 147 | + str += t + ":" + tags[t] + " "; |
| 148 | + } |
| 149 | + } |
| 150 | + return str; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function jsonFormatter(options) { |
| 155 | + return through(jsonify); |
| 156 | + |
| 157 | + function jsonify(logEvent) { |
| 158 | + if (options.timeStamp) { |
| 159 | + logEvent.time = new Date(logEvent.time).toISOString(); |
| 160 | + } else { |
| 161 | + delete logEvent.time; |
| 162 | + } |
| 163 | + logEvent.msg = logEvent.msg.toString(); |
| 164 | + this.emit("data", JSON.stringify(logEvent)); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +function lineMerger(host) { |
| 169 | + var previousLine = null; |
| 170 | + var flushTimer = null; |
| 171 | + var stream = through(lineMergerWrite, lineMergerEnd); |
| 172 | + var flush = _flush.bind(stream); |
| 173 | + |
| 174 | + return stream; |
| 175 | + |
| 176 | + function lineMergerWrite(line) { |
| 177 | + if (/^\s+/.test(line.msg)) { |
| 178 | + if (previousLine) { |
| 179 | + previousLine.msg += "\n" + line.msg; |
| 180 | + } else { |
| 181 | + previousLine = line; |
| 182 | + } |
| 183 | + } else { |
| 184 | + flush(); |
| 185 | + previousLine = line; |
| 186 | + } |
| 187 | + // rolling timeout |
| 188 | + clearTimeout(flushTimer); |
| 189 | + flushTimer = setTimeout(flush.bind(this), 10); |
| 190 | + } |
| 191 | + |
| 192 | + function _flush() { |
| 193 | + if (previousLine) { |
| 194 | + this.emit("data", previousLine); |
| 195 | + previousLine = null; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + function lineMergerEnd() { |
| 200 | + flush.call(this); |
| 201 | + this.emit("end"); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * Net new function in internal fork to replace duplexer |
| 207 | + */ |
| 208 | + |
| 209 | +function createDuplex(input: stream.Readable, output: stream.Writable) { |
| 210 | + const duplex = new stream.Duplex({ |
| 211 | + objectMode: false, |
| 212 | + allowHalfOpen: false, |
| 213 | + }); |
| 214 | + // Forward writes to the input stream |
| 215 | + duplex._write = (chunk, encoding, cb) => { |
| 216 | + input.write(chunk, encoding, cb); |
| 217 | + }; |
| 218 | + // Forward reads from the output stream |
| 219 | + duplex._read = () => { |
| 220 | + // This will be handled by the output stream's data events |
| 221 | + }; |
| 222 | + // Handle end of writing |
| 223 | + duplex._final = (cb) => { |
| 224 | + input.end(cb); |
| 225 | + }; |
| 226 | + // Pipe output stream data to our duplex stream |
| 227 | + output.on("data", (chunk) => { |
| 228 | + duplex.push(chunk); |
| 229 | + }); |
| 230 | + output.on("end", () => { |
| 231 | + duplex.push(null); // Signal end of stream |
| 232 | + }); |
| 233 | + // Forward errors |
| 234 | + input.on("error", (err) => { |
| 235 | + duplex.emit("error", err); |
| 236 | + }); |
| 237 | + output.on("error", (err) => { |
| 238 | + duplex.emit("error", err); |
| 239 | + }); |
| 240 | + return duplex; |
| 241 | +} |
0 commit comments