debug
debug copied to clipboard
How can I write to file without colors and to console with colors?
I made this function which console logs and writes to file but I'm not able to have the console with colors and the file output without colors
process.env.DEBUG = 'my_app:*'
process.env.DEBUG_COLORS = false
const debug = require('debug')
debug.log = (...args) => {
console.log(...args)
const file = 'my_file.txt'
const line = formatArgs(args)
fs.appendFileSync(file, line + '\n')
}
}
FORCE_COLOR=1
as an environment variable should do it.
FORCE_COLOR=1
as an environment variable should do it.
that doesn't seem to do anything
process.env.DEBUG = 'myapp:*'
process.env.DEBUG_COLORS = false
const debug = require('debug')('myapp:test')
const fs = require('fs')
process.env.FORCE_COLOR = 1
debug.log = (...args) => {
console.log(...args)
const file = 'logs/my_file.txt'
const line = JSON.stringify(args)
fs.appendFileSync(file, line + '\n')
}
debug('testing')
Set it as an actual environment variable. Color support has already been detected after you require debug.
Set it as an actual environment variable. Color support has already been detected after you require debug.
That still doesn't do anything
process.env.DEBUG = 'myapp:*'
process.env.DEBUG_COLORS = false
process.env.FORCE_COLOR = 1
const debug = require('debug')('myapp:test')
const fs = require('fs')
debug.log = (...args) => {
console.log(...args)
const file = 'logs/my_file.txt'
const line = JSON.stringify(args)
fs.appendFileSync(file, line + '\n')
}
debug('testing')
Even if I launch with FORCE_COLOR=1 node test
it doesn't print in color.
I filter it myself:
debug.log = (...args) => {
console.log(...args)
const file = 'log.txt'
args[0] = args[0].split('%c').join('') //remove%c
const line = util.inspect(
args.filter(item => {
if (!item || typeof item !== 'string') return true
return item.substring(0, 6) !== 'color:'
})
)
let d = new Date(Date.now() + 28800000)
.toJSON() //to 2019-04-20T06:20:54.513Z
.replace('T', ' ')
.substring(0, 23)
.split(/[-]/)
.join('') //time fromat: 20190704 21:30:02.489
fs.appendFileSync(file, d + ' ' + line + '\n')
}
output:
//20190704 21:30:02.492 [ 'main testing +2ms' ]
I am using this regexp:
const formatted = "\x1b[F\x1b[31;1mHello, there!\x1b[m\x1b[E";
const text = formatted.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
console.log(text);
Source: https://stackoverflow.com/a/29497680/251555
@ivosabev that's my answer on the SO question. Just use ansi-regex
.
ok