Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
How to Read a .log File in JS?

How can I read this log.log file?

It currently console.log's:

{"type":"Buffer","data":[65,116,116,101,109,112,116,101,100,32,116,111,32,100,105,118,105,100,101,32,98,121,32,122,101,114,111,46,10]}

const displayLogFile = () => {
  fs.readFile("./log.log", (err, file) => { //adding "utf-8" only logs a small portion of the .log file, but I was told this might be an async issue.
    console.log(file);
  });
};
log.log (local file):

Attempted to divide by zero.
Error: ENOENT: no such file or directory, open './NoFileNamedThis.txt'
    at Object.openSync (fs.js:498:3)
    at Object.readFileSync (fs.js:394:35)
    at fileDoesNotExist (C:\Users.js:33:6)
    at Object.<anonymous> (C:\Users:54:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path: './NoFileNamedThis.txt'
}
Index was outside the bounds of the array.
TypeError: Cannot read property '0' of null
    at arrayIsNull (C:\Users:44:6)
    at Object.<anonymous> (C:\Users:66:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

I would like to console.log() this .log, log file, but it console.log()'s an array buffer.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're not specifying an encoding, so the file gets read as a Buffer.

If no encoding is specified, then the raw buffer is returned.
If options is a string, then it specifies the encoding:
readFile('/etc/passwd', 'utf8', callback);

const displayLogFile = () => {
  fs.readFile("./log.log", "utf8", (err, file) => {
    console.log(file);
  });
};

to have it decoded into a string (assuming it's encoded in UTF-8).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda