Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const colors = require('colors/safe');
const { format } = require('logform');
const { LEVEL } = require('triple-beam');
const attributeRegex = /%attribute%/gi;
const colorizeExtra = format((info, opts) => {
// The immutable level string of the message (the normal property could be mutated already)
let lev = info[LEVEL];
// colors enables multiple styles separated by spaces
let colorStyles = opts.colors[lev].split(' ');
for (let key of Object.keys(info)) {
if (info[key] !== undefined && (opts.all || opts[key])) {
// surround the value with the style codes one by one
for (let style of colorStyles) {
try {
info[key] = colors[style](info[key]);
} catch (e) {
// silent fail, can't log here
}
}
}
if (info[key] !== undefined && (opts.all || opts[key])) {
// surround the value with the style codes one by one
for (let style of colorStyles) {
try {
info[key] = colors[style](info[key]);
} catch (e) {
// silent fail, can't log here
}
}
}
}
return info;
});
const padLevelExtra = format(info => {
let padding = ' '.repeat(Math.max(5 - info[LEVEL].length, 0));
info.level = `${info.level}${padding}`;
return info;
});
const attributeFormat = format((info, opts) => {
for (let key of Object.keys(info)) {
if (typeof opts[key] === 'string') {
if (typeof info[key] !== 'string') {
info[key] = JSON.stringify(info[key]);
}
info[key] = opts[key].replace(attributeRegex, info[key]);
}
}
const { inspect } = require('util')
const { MESSAGE, SPLAT } = require('triple-beam')
const winston = require('winston')
const { format } = require('logform')
const formatLogMessage = format((info, opts) => {
const depth = opts.depth || null
if (info[SPLAT]) {
for (const splat of info[SPLAT]) {
info.message += '\n' + inspect(splat, false, depth, opts.colorize)
}
}
info[MESSAGE] = `${info.level}:${info.message}`
return info
})
// Creates a new logger with the supplied options.
// Options are:
// - colorize - whethers to colourise the output
// - level - the logging level
}
}
}
}
return info;
});
const padLevelExtra = format(info => {
let padding = ' '.repeat(Math.max(5 - info[LEVEL].length, 0));
info.level = `${info.level}${padding}`;
return info;
});
const attributeFormat = format((info, opts) => {
for (let key of Object.keys(info)) {
if (typeof opts[key] === 'string') {
if (typeof info[key] !== 'string') {
info[key] = JSON.stringify(info[key]);
}
info[key] = opts[key].replace(attributeRegex, info[key]);
}
}
return info;
});
module.exports.ColorizerExtra = colorizeExtra;
module.exports.PadLevelExtra = padLevelExtra;
module.exports.AttributeFormat = attributeFormat;