Socket
Socket
Sign inDemoInstall

@spinajs/log

Package Overview
Dependencies
Maintainers
1
Versions
277
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@spinajs/log - npm Package Compare versions

Comparing version 1.2.79 to 1.2.81

5

lib/log.d.ts
import { Container, SyncModule } from "@spinajs/di";
import { ILogTargetDesc } from "./targets/LogTarget";
import { ILogOptions, ILogRule, ILogEntry, LogVariables, ILog } from "@spinajs/log-common";
import { ILogOptions, ILogRule, ILogEntry, LogVariables, ILog, ILogTargetDesc } from "@spinajs/log-common";
/**

@@ -18,2 +17,3 @@ * Default log implementation interface. Taken from bunyan. Feel free to implement own.

Timers: Map<string, Date>;
Targets: ILogTargetDesc[];
static clearLoggers(): void;

@@ -23,3 +23,2 @@ protected static AttachedToExitEvents: boolean;

protected Rules: ILogRule[];
protected Targets: ILogTargetDesc[];
protected Variables: Record<string, any>;

@@ -26,0 +25,0 @@ protected Container: Container;

@@ -24,31 +24,2 @@ declare const CONFIGURATION_SCHEMA: {

};
theme: {
type: string;
properties: {
security: {
type: string;
};
fatal: {
type: string;
};
error: {
type: string;
};
warn: {
type: string;
};
success: {
type: string;
};
info: {
type: string;
};
debug: {
type: string;
};
trace: {
type: string;
};
};
};
enabled: {

@@ -55,0 +26,0 @@ type: string;

@@ -26,31 +26,2 @@ "use strict";

},
theme: {
type: "object",
properties: {
security: {
type: "string",
},
fatal: {
type: "string",
},
error: {
type: "string",
},
warn: {
type: "string",
},
success: {
type: "string",
},
info: {
type: "string",
},
debug: {
type: "string",
},
trace: {
type: "string",
},
},
},
enabled: {

@@ -57,0 +28,0 @@ type: "boolean",

3

lib/targets/BlackHoleTarget.d.ts

@@ -1,3 +0,2 @@

import { ICommonTargetOptions } from "@spinajs/log-common";
import { LogTarget } from "./LogTarget";
import { ICommonTargetOptions, LogTarget } from "@spinajs/log-common";
/**

@@ -4,0 +3,0 @@ * Empty writer, usefull for tests or when we dont want to get any messages

@@ -10,8 +10,8 @@ "use strict";

exports.BlackHoleTarget = void 0;
const log_common_1 = require("@spinajs/log-common");
const di_1 = require("@spinajs/di");
const LogTarget_1 = require("./LogTarget");
/**
* Empty writer, usefull for tests or when we dont want to get any messages
*/
let BlackHoleTarget = class BlackHoleTarget extends LogTarget_1.LogTarget {
let BlackHoleTarget = class BlackHoleTarget extends log_common_1.LogTarget {
async write() {

@@ -18,0 +18,0 @@ return;

@@ -1,3 +0,3 @@

import { IColoredConsoleTargetOptions, ILogEntry } from "@spinajs/log-common";
import { LogTarget } from "./LogTarget";
import { IColoredConsoleTargetOptions, ILogEntry, LogTarget } from "@spinajs/log-common";
import { Chalk } from "chalk";
export declare const DEFAULT_THEME: {

@@ -14,2 +14,3 @@ security: string[];

export declare class ColoredConsoleTarget extends LogTarget<IColoredConsoleTargetOptions> {
protected theme: Chalk[];
protected StdConsoleCallbackMap: {

@@ -49,4 +50,4 @@ 5: {

};
resolve(): void;
resolve(): Promise<void>;
write(data: ILogEntry): Promise<void>;
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

@@ -24,8 +8,4 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;

};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};

@@ -36,5 +16,4 @@ Object.defineProperty(exports, "__esModule", { value: true });

const di_1 = require("@spinajs/di");
const LogTarget_1 = require("./LogTarget");
const __1 = require("..");
const colors = __importStar(require("colors/safe"));
const chalk_1 = __importDefault(require("chalk"));
const configuration_1 = require("@spinajs/configuration");

@@ -51,5 +30,6 @@ exports.DEFAULT_THEME = {

};
let ColoredConsoleTarget = class ColoredConsoleTarget extends LogTarget_1.LogTarget {
let ColoredConsoleTarget = class ColoredConsoleTarget extends log_common_1.LogTarget {
constructor() {
super(...arguments);
this.theme = [];
this.StdConsoleCallbackMap = {

@@ -66,5 +46,11 @@ [__1.LogLevel.Error]: console.error,

}
resolve() {
var _a;
colors.setTheme((_a = this.Options.theme) !== null && _a !== void 0 ? _a : exports.DEFAULT_THEME);
async resolve() {
this.theme[__1.LogLevel.Trace] = chalk_1.default.gray;
this.theme[__1.LogLevel.Debug] = chalk_1.default.gray;
this.theme[__1.LogLevel.Info] = chalk_1.default.white;
this.theme[__1.LogLevel.Success] = chalk_1.default.white.bgGreen;
this.theme[__1.LogLevel.Warn] = chalk_1.default.yellow;
this.theme[__1.LogLevel.Error] = chalk_1.default.red;
this.theme[__1.LogLevel.Fatal] = chalk_1.default.white.bgRed;
this.theme[__1.LogLevel.Security] = chalk_1.default.yellow.bgRed;
super.resolve();

@@ -81,3 +67,3 @@ }

/* eslint-disable */
colors[log_common_1.LogLevelStrings[data.Level]]((0, configuration_1.format)(data.Variables, this.Options.layout)));
this.theme[log_common_1.LogLevelStrings[data.Level]]((0, configuration_1.format)(data.Variables, this.Options.layout)));
}

@@ -84,0 +70,0 @@ };

/// <reference types="node" />
import { LogTarget } from "./LogTarget";
import { IFileTargetOptions, ILogEntry } from "@spinajs/log-common";
import { IFileTargetOptions, ILogEntry, LogTarget } from "@spinajs/log-common";
import * as fs from "fs";
import { Job } from "node-schedule";
import { Readable } from "stream";
export declare class FileTarget extends LogTarget<IFileTargetOptions> {
protected LogDirPath: string;
protected LogFileName: string;
protected LogPath: string;
protected LogFileExt: string;
protected LogBaseName: string;
protected ArchiveDirPath: string;
LogDirPath: string;
LogFileName: string;
LogPath: string;
LogFileExt: string;
LogBaseName: string;
ArchiveDirPath: string;
protected RotateJob: Job;
protected ArchiveJob: Job;
protected CurrentFileSize: number;
protected BufferSize: number;
protected FlushTimeout: NodeJS.Timeout;
protected Buffer: any[];
protected IsArchiving: boolean;
protected WriteStream: fs.WriteStream;
resolve(): void;
protected ReadStream: Readable;
resolve(): Promise<void>;
write(data: ILogEntry): Promise<void>;

@@ -25,3 +23,4 @@ protected archive(): void;

private initialize;
private reopenStream;
private close;
private open;
}

@@ -33,5 +33,6 @@ "use strict";

exports.FileTarget = void 0;
const os_1 = require("os");
/* eslint security/detect-non-literal-fs-filename:0 -- Safe as no value holds user input */
const di_1 = require("@spinajs/di");
const LogTarget_1 = require("./LogTarget");
const log_common_1 = require("@spinajs/log-common");
const fs = __importStar(require("fs"));

@@ -41,14 +42,12 @@ const path = __importStar(require("path"));

const exceptions_1 = require("@spinajs/exceptions");
const os_1 = require("os");
const glob = __importStar(require("glob"));
const zlib = __importStar(require("zlib"));
const configuration_1 = require("@spinajs/configuration");
const console_1 = require("console");
let FileTarget = class FileTarget extends LogTarget_1.LogTarget {
const stream_1 = require("stream");
let FileTarget = class FileTarget extends log_common_1.LogTarget {
constructor() {
super(...arguments);
this.BufferSize = 0;
this.Buffer = [];
this.IsArchiving = false;
}
resolve() {
async resolve() {
this.Options.options = Object.assign({

@@ -58,5 +57,8 @@ compress: true,

maxArchiveFiles: 5,
bufferSize: 8 * 1024,
flushTimeout: 10 * 1000,
}, 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.initialize();

@@ -66,20 +68,11 @@ this.rotate();

}
write(data) {
async write(data) {
if (!this.Options.enabled) {
return;
}
const result = (0, configuration_1.format)(data.Variables, this.Options.layout) + os_1.EOL;
this.CurrentFileSize += Buffer.byteLength(result);
(0, console_1.assert)(this.WriteStream, "write stream is not created");
const ok = this.WriteStream.write(result, (err) => {
if (err) {
console.debug(`Cannot write to log stream at path ${this.LogPath}`);
}
});
if (!ok) {
this.WriteStream.once("drain", () => {
void this.write(data);
});
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) {
if (this.CurrentFileSize >= this.Options.options.maxSize && !this.IsArchiving) {
this.archive();

@@ -89,2 +82,3 @@ }

archive() {
this.IsArchiving = true;
const files = glob

@@ -101,2 +95,3 @@ .sync(path.join(this.ArchiveDirPath, `archived_${this.LogBaseName}*{${this.LogFileExt},.gzip}`))

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}`);

@@ -106,25 +101,53 @@ if (!fs.existsSync(this.LogPath)) {

}
fs.copyFileSync(this.LogPath, archPath);
fs.unlinkSync(this.LogPath);
this.initialize();
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);
read.pipe(zip).pipe(write);
write.on("finish", () => {
read.close();
zip.close();
write.close();
fs.unlink(archPath, () => {
return;
});
this.WriteStream.once("close", () => {
fs.rename(this.LogPath, renPath, (err) => {
if (!err) {
fs.copyFile(renPath, archPath, (err) => {
if (!err) {
fs.unlink(this.LogPath, () => {
this.initialize();
});
}
else {
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, () => {
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, () => {
return;
});
}
});
}
else {
this.initialize();
}
});
}
if (files.length >= this.Options.options.maxArchiveFiles) {
fs.unlink(files[0].name, () => {
return;
});
}
});
this.close();
}

@@ -140,5 +163,6 @@ rotate() {

this.CurrentFileSize = 0;
this.IsArchiving = false;
this.LogDirPath = path.dirname(path.resolve((0, configuration_1.format)(null, this.Options.options.path)));
this.ArchiveDirPath = this.Options.options.archivePath ? path.resolve((0, configuration_1.format)(null, this.Options.options.archivePath)) : this.LogDirPath;
this.LogFileName = (0, configuration_1.format)(null, path.basename(this.Options.options.path));
this.LogFileName = (0, configuration_1.format)({ logger: this.Options.name }, path.basename(this.Options.options.path));
this.LogPath = path.join(this.LogDirPath, this.LogFileName);

@@ -159,12 +183,15 @@ const { name, ext } = path.parse(this.LogFileName);

}
this.reopenStream();
try {
const { size } = fs.statSync(this.LogPath);
this.CurrentFileSize = size;
}
catch (_a) { }
this.open();
}
reopenStream() {
if (this.WriteStream) {
this.WriteStream.removeAllListeners();
this.WriteStream.close((err) => {
console.debug(`Cannot close log write stream ${this.LogPath}, reason: ${err.message}`);
});
this.WriteStream = null;
}
close() {
this.ReadStream.unpipe(this.WriteStream);
this.WriteStream.end();
this.WriteStream = null;
}
open() {
this.WriteStream = fs.createWriteStream(this.LogPath, {

@@ -174,11 +201,15 @@ flags: "a",

});
this.WriteStream.on("open", () => {
console.debug(`Opend log write stream at ${this.LogPath}`);
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);
});
this.WriteStream.on("error", (err) => {
console.debug(`Cannot create log file write steram at path ${this.LogPath}, reason: ${err.message}`);
this.reopenStream();
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);
});
this.WriteStream.on("close", () => {
console.debug(`Closed log write stream at ${this.LogPath}`);
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);
setTimeout(() => {
this.initialize();
}, 1000);
});

@@ -185,0 +216,0 @@ }

export * from "./BlackHoleTarget";
export * from "./ColoredConsoleTarget";
export * from "./FileTarget";
export * from "./LogTarget";

@@ -20,3 +20,2 @@ "use strict";

__exportStar(require("./FileTarget"), exports);
__exportStar(require("./LogTarget"), exports);
//# sourceMappingURL=index.js.map
{
"name": "@spinajs/log",
"version": "1.2.79",
"version": "1.2.81",
"description": "Log lib for all spinejs related libs",

@@ -29,8 +29,9 @@ "main": "lib/index.js",

"dependencies": {
"@spinajs/configuration": "^1.2.79",
"@spinajs/di": "^1.2.79",
"@spinajs/exceptions": "^1.2.79",
"@spinajs/log-common": "^1.2.79",
"@colors/colors": "^1.5.0",
"@spinajs/configuration": "^1.2.81",
"@spinajs/di": "^1.2.81",
"@spinajs/exceptions": "^1.2.81",
"@spinajs/log-common": "^1.2.81",
"ajv": "^8.8.2",
"colors": "^1.4.0",
"chalk": "4.1.2",
"glob": "^7.2.0",

@@ -43,5 +44,6 @@ "glob-to-regexp": "^0.4.1",

"devDependencies": {
"@types/color": "^3.0.3"
"@types/mock-fs": "^4.13.1",
"mock-fs": "^5.1.2"
},
"gitHead": "ae595f5daffb88372496b6e5bf38973f07e33683"
"gitHead": "dd58385e770069586eb8814a13365ce889d46b34"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc