如何在 JS 中读取 .log 文件?
How to Read a .log File in JS?
我如何阅读这个 log.log 文件?
目前console.log的:
{"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(本地文件):
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
我想 console.log() 这个 .log 日志文件,但它 console.log() 是一个数组缓冲区。
您不是 specifying an encoding,因此文件被作为缓冲区读取。
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);
});
};
将其解码为字符串(假设它以 UTF-8 编码)。
我如何阅读这个 log.log 文件?
目前console.log的:
{"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);
});
};
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
我想 console.log() 这个 .log 日志文件,但它 console.log() 是一个数组缓冲区。
您不是 specifying an encoding,因此文件被作为缓冲区读取。
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);
});
};
将其解码为字符串(假设它以 UTF-8 编码)。