@spinajs/log
Advanced tools
Comparing version 1.2.59 to 1.2.60
export * from "@spinajs/log-common"; | ||
export * from "./targets"; | ||
export * from "./variables"; | ||
export * from "./log"; | ||
export * from "./decorators"; | ||
export * from "./bootstrap"; |
@@ -15,3 +15,2 @@ "use strict"; | ||
__exportStar(require("./targets"), exports); | ||
__exportStar(require("./variables"), exports); | ||
__exportStar(require("./log"), exports); | ||
@@ -18,0 +17,0 @@ __exportStar(require("./decorators"), exports); |
@@ -15,2 +15,4 @@ "use strict"; | ||
const colors = require("colors/safe"); | ||
const configuration_1 = require("@spinajs/configuration"); | ||
const internal_logger_1 = require("@spinajs/internal-logger"); | ||
exports.DEFAULT_THEME = { | ||
@@ -43,2 +45,3 @@ security: ["red", "bgBrightWhite"], | ||
colors.setTheme((_a = this.Options.theme) !== null && _a !== void 0 ? _a : exports.DEFAULT_THEME); | ||
internal_logger_1.InternalLogger.debug(`Created console log target`, "file-target"); | ||
super.resolve(); | ||
@@ -55,3 +58,3 @@ } | ||
/* eslint-disable */ | ||
colors[log_common_1.LogLevelStrings[data.Level]](this.format(data.Variables, this.Options.layout))); | ||
colors[log_common_1.LogLevelStrings[data.Level]]((0, configuration_1.format)(data.Variables, this.Options.layout))); | ||
} | ||
@@ -58,0 +61,0 @@ }; |
"use strict"; | ||
/* eslint security/detect-non-literal-fs-filename:0 -- Safe as no value holds user input */ | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -11,2 +10,4 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
exports.FileTarget = void 0; | ||
const internal_logger_1 = require("@spinajs/internal-logger"); | ||
/* eslint security/detect-non-literal-fs-filename:0 -- Safe as no value holds user input */ | ||
const di_1 = require("@spinajs/di"); | ||
@@ -21,2 +22,3 @@ const LogTarget_1 = require("./LogTarget"); | ||
const zlib = require("zlib"); | ||
const configuration_1 = require("@spinajs/configuration"); | ||
let FileTarget = class FileTarget extends LogTarget_1.LogTarget { | ||
@@ -50,3 +52,3 @@ constructor() { | ||
} | ||
const result = this.format(data.Variables, this.Options.layout) + os_1.EOL; | ||
const result = (0, configuration_1.format)(data.Variables, this.Options.layout) + os_1.EOL; | ||
const bytes = Buffer.byteLength(result); | ||
@@ -59,2 +61,3 @@ this.BufferSize += bytes; | ||
if (this.CurrentFileSize > this.Options.options.maxSize) { | ||
internal_logger_1.InternalLogger.debug(`File ${this.LogPath} exceeded log size ( current: ${this.CurrentFileSize}, max: ${this.Options.options.maxSize})`, "file-target"); | ||
this.archive(); | ||
@@ -74,5 +77,3 @@ } | ||
const newestFile = files.length !== 0 ? files[files.length - 1].name : undefined; | ||
const fIndex = newestFile | ||
? parseInt(newestFile.substring(newestFile.lastIndexOf("_") + 1, newestFile.lastIndexOf("_") + 2), 10) + 1 | ||
: 1; | ||
const fIndex = newestFile ? parseInt(newestFile.substring(newestFile.lastIndexOf("_") + 1, newestFile.lastIndexOf("_") + 2), 10) + 1 : 1; | ||
const archPath = path.join(this.ArchiveDirPath, `archived_${this.LogBaseName}_${fIndex}${this.LogFileExt}`); | ||
@@ -101,4 +102,9 @@ this.flush(); | ||
}); | ||
internal_logger_1.InternalLogger.debug(`Created archived compressed file at ${zippedPath}`, "file-target"); | ||
} | ||
else { | ||
internal_logger_1.InternalLogger.debug(`Created archived file at ${archPath}`, "file-target"); | ||
} | ||
if (files.length >= this.Options.options.maxArchiveFiles) { | ||
internal_logger_1.InternalLogger.debug(`Deleting old archive file ${files[0].name}`, "file-target"); | ||
fs.unlink(files[0].name, () => { | ||
@@ -119,2 +125,3 @@ return; | ||
rotate() { | ||
internal_logger_1.InternalLogger.debug(`Initializing rotating log files at schedule ${this.Options.options.rotate}`, "file-target"); | ||
if (this.Options.options.rotate) { | ||
@@ -133,7 +140,5 @@ this.RotateJob = (0, node_schedule_1.scheduleJob)(`LogScheduleJob`, this.Options.options.rotate, () => { | ||
this.CurrentFileSize = 0; | ||
this.LogDirPath = this.format(null, path.dirname(path.resolve(this.Options.options.path))); | ||
this.ArchiveDirPath = this.Options.options.archivePath | ||
? this.format(null, path.resolve(this.Options.options.archivePath)) | ||
: this.LogDirPath; | ||
this.LogFileName = this.format(null, path.basename(this.Options.options.path)); | ||
this.LogDirPath = (0, configuration_1.format)(null, path.dirname(path.resolve(this.Options.options.path))); | ||
this.ArchiveDirPath = this.Options.options.archivePath ? (0, configuration_1.format)(null, path.resolve(this.Options.options.archivePath)) : this.LogDirPath; | ||
this.LogFileName = (0, configuration_1.format)(null, path.basename(this.Options.options.path)); | ||
this.LogPath = path.join(this.LogDirPath, this.LogFileName); | ||
@@ -164,2 +169,3 @@ const { name, ext } = path.parse(this.LogFileName); | ||
} | ||
internal_logger_1.InternalLogger.debug(`Creating log file at path ${this.LogPath}`, "file-target"); | ||
this.HasError = false; | ||
@@ -166,0 +172,0 @@ this.Error = null; |
import { SyncModule } from "@spinajs/di"; | ||
import { LogVariable, ILogEntry, ICommonTargetOptions, LogVariables, ILogRule, ITargetsOption } from "@spinajs/log-common"; | ||
import { ILogEntry, ICommonTargetOptions, ILogRule, ITargetsOption } from "@spinajs/log-common"; | ||
export interface ILogTargetDesc { | ||
@@ -9,12 +9,7 @@ instance: LogTarget<ICommonTargetOptions>; | ||
export declare abstract class LogTarget<T extends ICommonTargetOptions> extends SyncModule { | ||
protected Variables: LogVariable[]; | ||
HasError: boolean; | ||
Error: Error | null | unknown; | ||
protected VariablesDictionary: Map<string, LogVariable>; | ||
protected LayoutRegexp: RegExp; | ||
protected Options: T; | ||
constructor(Variables: LogVariable[], options: T); | ||
constructor(options: T); | ||
abstract write(data: ILogEntry): Promise<void>; | ||
protected format(customVars: LogVariables | null, layout: string): string; | ||
protected _format(vars: LogVariables, txt: string): string; | ||
} |
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -15,64 +6,16 @@ exports.LogTarget = void 0; | ||
const _ = require("lodash"); | ||
const log_common_1 = require("@spinajs/log-common"); | ||
let LogTarget = class LogTarget extends di_1.SyncModule { | ||
constructor(Variables, options) { | ||
class LogTarget extends di_1.SyncModule { | ||
constructor(options) { | ||
super(); | ||
this.Variables = Variables; | ||
this.HasError = false; | ||
this.Error = null; | ||
this.VariablesDictionary = new Map(); | ||
this.Variables.forEach((v) => { | ||
this.VariablesDictionary.set(v.Name, v); | ||
}); | ||
this.LayoutRegexp = /\{((.*?)(:(.*?))?)\}/gm; | ||
if (options) { | ||
this.Options = _.merge(_.merge(this.Options, { | ||
enabled: true, | ||
layout: "{datetime} {level} {message} {error} ({logger})", | ||
layout: "${datetime} ${level} ${message} ${error} (${logger})", | ||
}), options); | ||
} | ||
} | ||
format(customVars, layout) { | ||
if (customVars === null || customVars === void 0 ? void 0 : customVars.message) { | ||
return this._format(Object.assign(Object.assign({}, customVars), { message: this._format(customVars, customVars.message) }), layout); | ||
} | ||
return this._format(customVars, layout); | ||
} | ||
_format(vars, txt) { | ||
this.LayoutRegexp.lastIndex = 0; | ||
const varMatch = [...txt.matchAll(this.LayoutRegexp)]; | ||
if (!varMatch) { | ||
return ""; | ||
} | ||
let result = txt; | ||
varMatch.forEach((v) => { | ||
var _a, _b; | ||
if (vars && vars[v[2]]) { | ||
const fVar = vars[v[2]]; | ||
if (fVar instanceof Function) { | ||
result = result.replace(v[0], fVar((_a = v[4]) !== null && _a !== void 0 ? _a : null)); | ||
} | ||
else { | ||
result = result.replace(v[0], fVar); | ||
} | ||
} | ||
else { | ||
const variable = this.VariablesDictionary.get(v[2]); | ||
if (variable) { | ||
// optional parameter eg. {env:PORT} | ||
result = result.replace(v[0], variable.Value((_b = v[4]) !== null && _b !== void 0 ? _b : null)); | ||
} | ||
else { | ||
result = result.replace(v[0], ""); | ||
} | ||
} | ||
}); | ||
return result; | ||
} | ||
}; | ||
LogTarget = __decorate([ | ||
(0, di_1.Inject)(Array.ofType(log_common_1.LogVariable)), | ||
__metadata("design:paramtypes", [Array, Object]) | ||
], LogTarget); | ||
} | ||
exports.LogTarget = LogTarget; | ||
//# sourceMappingURL=LogTarget.js.map |
{ | ||
"name": "@spinajs/log", | ||
"version": "1.2.59", | ||
"version": "1.2.60", | ||
"description": "Log lib for all spinejs related libs", | ||
@@ -29,6 +29,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@spinajs/configuration": "^1.2.59", | ||
"@spinajs/configuration": "^1.2.60", | ||
"@spinajs/di": "^1.2.32", | ||
"@spinajs/exceptions": "^1.2.7", | ||
"@spinajs/log-common": "^1.2.49", | ||
"@spinajs/log-common": "^1.2.60", | ||
"ajv": "^8.8.2", | ||
@@ -45,3 +45,3 @@ "colors": "^1.4.0", | ||
}, | ||
"gitHead": "b96e59b558c2a7d5758f886ce7f856888078ddd9" | ||
"gitHead": "d29df7b43d1c135b075569f94d079be323ff2ba4" | ||
} |
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
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
191702
1422