Skip to content

Commit

Permalink
update types
Browse files Browse the repository at this point in the history
  • Loading branch information
LCluber committed Dec 13, 2024
1 parent 57c69b7 commit daa9e39
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ Any other value (like "dev" or "development") will set the log level to **debug*
export type Levels = 'error'|'warn'|'info'|'debug';

export type Options = {
timeZone: string,
locale: string,
serviceName: string,
level: Levels
timeZone: string;
locale: string;
serviceName: string;
level: Levels;
};

init(options: Options): void {}
Expand Down
10 changes: 8 additions & 2 deletions dist/winstan.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ SOFTWARE.
https://github.com/DWTechs/Winstan.js
*/

import type { Logger } from "winston";
import type { Logger, Logform } from "winston";
export type Levels = 'error' | 'warn' | 'info' | 'debug';
declare function init(timeZone: string, serviceName: string, level: Levels): void;
export type Options = {
timeZone: string;
locale: string;
serviceName: string;
level: Levels;
};
declare function init(options: Options): Logform.Format;
declare const log: () => Logger;

export {
Expand Down
35 changes: 19 additions & 16 deletions dist/winstan.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ https://github.com/DWTechs/Winstan.js
*/

import * as winston from 'winston';
import { isStringOfLength, isProperty } from '@dwtechs/checkard';
import { isProperty, isStringOfLength } from '@dwtechs/checkard';

let defaultSN = "serviceName";
let defaultSN = "";
let defaultLocale = "fr-FR";
let defaultTZ = "europe/paris";
let defaultNodeEnv = "development";
Expand All @@ -46,33 +46,36 @@ const levels = {
debug: 3,
};
let lvl = defaultLvl;
let fmt = setFormat(defaultTZ, "serviceName");
const tpts = setTransports();
function getTimezonedDate(timeZone) {
return new Date().toLocaleString(defaultLocale, { timeZone });
}
function getFormatDate(timeZone) {
return isStringOfLength(timeZone, 2, 999) ? getTimezonedDate(timeZone) : getTimezonedDate(defaultTZ);
let fmt = init({ timeZone: defaultTZ,
locale: defaultLocale,
serviceName: defaultSN,
level: defaultLvl
});
function setDateFormat(timeZone, locale) {
timeZone = isStringOfLength(timeZone, 2, 999) ? timeZone : defaultTZ;
locale = isStringOfLength(locale, 5, 5) ? locale : defaultLocale;
return new Date().toLocaleString(locale, { timeZone });
}
function setServiceName(serviceName) {
return isStringOfLength(serviceName, 1, 999) ? serviceName : defaultSN;
}
function setTransports() {
return [new winston.transports.Console()];
}
function setFormat(tz, serviceName) {
const dateFormat = getFormatDate(tz);
const sn = setServiceName(serviceName);
const snLog = sn ? `${sn} ` : "";
return winston.format.combine(winston.format.colorize({ all: true }), winston.format.timestamp({ format: dateFormat }), winston.format.align(), winston.format.printf((info) => {
function setFormat(format, serviceName) {
const snLog = serviceName ? `${serviceName} ` : "";
return winston.format.combine(winston.format.colorize({ all: true }), winston.format.timestamp({ format }), winston.format.align(), winston.format.printf((info) => {
var _a;
const msg = (_a = info.message) === null || _a === void 0 ? void 0 : _a.toString().replace(/[\n\r]+/g, "").replace(/\s{2,}/g, " ");
return `[${info.timestamp}] - ${snLog}${info.level}: ${msg}`;
}));
}
function init(timeZone, serviceName, level) {
lvl = isProperty(level, levels) ? level : lvl;
fmt = setFormat(timeZone, serviceName);
function init(options) {
lvl = isProperty(options.level, levels) ? options.level : lvl;
const dateFormat = setDateFormat(options.timeZone, options.locale);
const sn = setServiceName(options.serviceName);
return setFormat(dateFormat, sn);
}
const log = () => {
return winston.createLogger({
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Levels = 'error'|'warn'|'info'|'debug';
export type Options = {
timeZone: string,
locale: string,
serviceName: string,
level: Levels
timeZone: string;
locale: string;
serviceName: string;
level: Levels;
};

0 comments on commit daa9e39

Please sign in to comment.