@spinajs/log
Advanced tools
Comparing version 1.2.85 to 1.2.88
@@ -6,2 +6,7 @@ /// <reference types="node" /> | ||
import { Readable } from "stream"; | ||
declare class FileLogStream extends Readable { | ||
protected LogQueue: string[]; | ||
enqueue(entry: string): void; | ||
_read(): void; | ||
} | ||
export declare class FileTarget extends LogTarget<IFileTargetOptions> { | ||
@@ -18,3 +23,3 @@ LogDirPath: string; | ||
protected WriteStream: fs.WriteStream; | ||
protected ReadStream: Readable; | ||
protected LogStream: FileLogStream; | ||
resolve(): Promise<void>; | ||
@@ -28,1 +33,2 @@ write(data: ILogEntry): Promise<void>; | ||
} | ||
export {}; |
@@ -45,2 +45,21 @@ "use strict"; | ||
const stream_1 = require("stream"); | ||
class FileLogStream extends stream_1.Readable { | ||
constructor() { | ||
super(...arguments); | ||
this.LogQueue = []; | ||
} | ||
enqueue(entry) { | ||
this.LogQueue.push(entry); | ||
} | ||
_read() { | ||
let ok = true; | ||
let val = null; | ||
do { | ||
val = this.LogQueue.shift(); | ||
if (val) { | ||
ok = this.push(val); | ||
} | ||
} while (ok && val); | ||
} | ||
} | ||
let FileTarget = class FileTarget extends log_common_1.LogTarget { | ||
@@ -57,7 +76,3 @@ constructor() { | ||
}, this.Options.options); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
this.ReadStream = new stream_1.Readable({ encoding: "utf-8", read: () => { } }); | ||
this.ReadStream.on("data", (chunk) => { | ||
this.CurrentFileSize += chunk.length; | ||
}); | ||
this.LogStream = new FileLogStream(); | ||
this.initialize(); | ||
@@ -71,7 +86,5 @@ this.rotate(); | ||
} | ||
const result = this.ReadStream.push((0, configuration_1.format)(data.Variables, this.Options.layout) + os_1.EOL); | ||
if (!result) { | ||
throw new Error(); | ||
} | ||
if (this.CurrentFileSize >= this.Options.options.maxSize && !this.IsArchiving) { | ||
const lEntry = (0, configuration_1.format)(data.Variables, this.Options.layout) + os_1.EOL; | ||
this.LogStream.enqueue(lEntry); | ||
if (this.CurrentFileSize + this.WriteStream.bytesWritten >= this.Options.options.maxSize && !this.IsArchiving) { | ||
this.archive(); | ||
@@ -93,3 +106,2 @@ } | ||
const fIndex = newestFile ? parseInt(newestFile.substring(newestFile.lastIndexOf("_") + 1, newestFile.lastIndexOf("_") + 2), 10) + 1 : 1; | ||
const renPath = this.LogPath + ".bck"; | ||
const archPath = path.join(this.ArchiveDirPath, `archived_${this.LogBaseName}_${fIndex}${this.LogFileExt}`); | ||
@@ -100,50 +112,28 @@ if (!fs.existsSync(this.LogPath)) { | ||
this.WriteStream.once("close", () => { | ||
fs.rename(this.LogPath, renPath, (err) => { | ||
fs.rename(this.LogPath, archPath, (err) => { | ||
this.initialize(); | ||
if (!err) { | ||
fs.copyFile(renPath, archPath, (err) => { | ||
if (!err) { | ||
fs.unlink(this.LogPath, () => { | ||
this.initialize(); | ||
}); | ||
} | ||
else { | ||
this.initialize(); | ||
return; | ||
} | ||
if (this.Options.options.compress) { | ||
const zippedPath = path.join(this.ArchiveDirPath, `archived_${this.LogBaseName}_${fIndex}${this.LogFileExt}.gzip`); | ||
const zip = zlib.createGzip(); | ||
const read = fs.createReadStream(archPath); | ||
const write = fs.createWriteStream(zippedPath); | ||
(0, stream_1.pipeline)(read, zip, write, (err) => { | ||
if (err) { | ||
this.ReadStream.push((0, configuration_1.format)((0, log_common_1.createLogMessageObject)(err, `Cannot compress log file at ${this.LogPath}`, log_common_1.LogLevel.Trace, "log-file-target", {}).Variables, this.Options.layout) + os_1.EOL); | ||
fs.unlink(zippedPath, () => { | ||
return; | ||
}); | ||
} | ||
fs.unlink(renPath, () => { | ||
if (this.Options.options.compress) { | ||
const zippedPath = path.join(this.ArchiveDirPath, `archived_${this.LogBaseName}_${fIndex}${this.LogFileExt}.gzip`); | ||
const zip = zlib.createGzip(); | ||
const read = fs.createReadStream(archPath); | ||
const write = fs.createWriteStream(zippedPath); | ||
(0, stream_1.pipeline)(read, zip, write, (err) => { | ||
if (err) { | ||
void this.write((0, log_common_1.createLogMessageObject)(err, `Cannot compress log file at ${this.LogPath}`, log_common_1.LogLevel.Trace, "log-file-target", {})); | ||
fs.unlink(zippedPath, () => { | ||
return; | ||
}); | ||
fs.unlink(archPath, () => { | ||
return; | ||
}); | ||
}); | ||
read.pipe(zip).pipe(write); | ||
write.on("finish", () => { | ||
read.close(); | ||
zip.close(); | ||
write.close(); | ||
}); | ||
} | ||
if (files.length >= this.Options.options.maxArchiveFiles) { | ||
fs.unlink(files[0].name, () => { | ||
} | ||
fs.unlink(archPath, () => { | ||
return; | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
if (files.length >= this.Options.options.maxArchiveFiles) { | ||
fs.unlink(files[0].name, () => { | ||
return; | ||
}); | ||
} | ||
} | ||
else { | ||
this.initialize(); | ||
} | ||
}); | ||
@@ -189,3 +179,3 @@ }); | ||
close() { | ||
this.ReadStream.unpipe(this.WriteStream); | ||
this.LogStream.unpipe(this.WriteStream); | ||
this.WriteStream.end(); | ||
@@ -199,12 +189,14 @@ this.WriteStream = null; | ||
}); | ||
this.ReadStream.pipe(this.WriteStream); | ||
this.WriteStream.once("open", () => { | ||
this.ReadStream.push((0, configuration_1.format)((0, log_common_1.createLogMessageObject)(`Log file opened at ${this.LogPath}`, [], log_common_1.LogLevel.Trace, "log-file-target", {}).Variables, this.Options.layout) + os_1.EOL); | ||
void this.write((0, log_common_1.createLogMessageObject)(`Log file opened at ${this.LogPath}`, [], log_common_1.LogLevel.Trace, "log-file-target", {})); | ||
if (this.WriteStream) { | ||
this.LogStream.pipe(this.WriteStream); | ||
} | ||
}); | ||
this.WriteStream.once("close", () => { | ||
this.ReadStream.push((0, configuration_1.format)((0, log_common_1.createLogMessageObject)(`Log file closed at ${this.LogPath}`, [], log_common_1.LogLevel.Trace, "log-file-target", {}).Variables, this.Options.layout) + os_1.EOL); | ||
void this.write((0, log_common_1.createLogMessageObject)(`Log file closed at ${this.LogPath}`, [], log_common_1.LogLevel.Trace, "log-file-target", {})); | ||
}); | ||
this.WriteStream.once("error", (err) => { | ||
this.ReadStream.unpipe(this.WriteStream); | ||
this.ReadStream.push((0, configuration_1.format)((0, log_common_1.createLogMessageObject)(err, `Cannot write to log file at ${this.LogPath}`, log_common_1.LogLevel.Error, "log-file-target", {}).Variables, this.Options.layout) + os_1.EOL); | ||
this.LogStream.unpipe(this.WriteStream); | ||
void this.write((0, log_common_1.createLogMessageObject)(err, `Cannot write to log file at ${this.LogPath}`, log_common_1.LogLevel.Error, "log-file-target", {})); | ||
setTimeout(() => { | ||
@@ -211,0 +203,0 @@ this.initialize(); |
{ | ||
"name": "@spinajs/log", | ||
"version": "1.2.85", | ||
"version": "1.2.88", | ||
"description": "Log lib for all spinejs related libs", | ||
@@ -46,3 +46,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "06030775e7704de5eceb11c3e6951a6038e76d7b" | ||
"gitHead": "7c42b937cd76ac16d6af25d7d503eb1a93ce5271" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61703
1016