question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yooducommented, Feb 10, 2021

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' ]
0reactions
heteocommented, May 5, 2021

ok

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write stdout to file with colors? - Stack Overflow
Usually the program doing the writing determines whether it's writing to a terminal, and if it's not it won't use colours. So it's...
Read more >
How to Write Stdout to a File with Colors | Baeldung on Linux
In this tutorial, we'll cover some tools we can use to write standard output to files while preserving their colors.
Read more >
Maintain color from script output for later viewing
It makes sense that the color isn't saved since the resulting file is just plain text, but is there any way I can...
Read more >
linux - When reading a file with `less` or `more`, how can I get ...
First produce a colorised sample (e.g. ccze -A </var/log/dpkg. log , ls -1 --color /var/log ) then pipe it to most : ls...
Read more >
fatih/color: Color package for Go (golang) - GitHub
// Use handy standard colors color.Set(color.FgYellow) fmt.Println("Existing text will now be in yellow") ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found