debug icon indicating copy to clipboard operation
debug copied to clipboard

How can I write to file without colors and to console with colors?

Open ghost opened this issue 5 years ago • 8 comments

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')
  }
}

ghost avatar Apr 11 '19 13:04 ghost

FORCE_COLOR=1 as an environment variable should do it.

Qix- avatar Apr 11 '19 16:04 Qix-

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')

ghost avatar Apr 11 '19 17:04 ghost

Set it as an actual environment variable. Color support has already been detected after you require debug.

Qix- avatar Apr 11 '19 20:04 Qix-

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.

ghost avatar Apr 12 '19 07:04 ghost

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' ]

yoodu avatar Jul 04 '19 13:07 yoodu

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 avatar Jan 30 '20 13:01 ivosabev

@ivosabev that's my answer on the SO question. Just use ansi-regex.

Qix- avatar Jan 30 '20 16:01 Qix-

ok

heteo avatar May 05 '21 01:05 heteo