0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to say that I already tried that, it only logs "Attempted to divide by zero.\n", which is like 1/10 of the entire .log file.
Are you sure about that? readFile "asynchronously reads the entire contents of a file."
I will check to see if its an asynchronous error. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.