Task: Fixed ts common package
This commit is contained in:
parent
e26ee50faf
commit
596d6e92d2
@ -3,6 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"description": "Typescript common and web utils",
|
||||
"main": "dist/index.js",
|
||||
"source": "src/index.ts",
|
||||
"scripts": {
|
||||
"start": "tsc -w",
|
||||
"build": "rm -rf dist && tsc",
|
||||
@ -39,4 +40,4 @@
|
||||
"lint-staged": {
|
||||
"**/*": "prettier --write --ignore-unknown"
|
||||
}
|
||||
}
|
||||
}
|
30
src/index.ts
30
src/index.ts
@ -1,16 +1,16 @@
|
||||
// import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date"
|
||||
// import { debug, info,error, warn } from './log'
|
||||
// import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser"
|
||||
import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date"
|
||||
import { debug, info, error, warn } from "./log"
|
||||
import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser"
|
||||
|
||||
// export {
|
||||
// formatDate,
|
||||
// formatDateOnly,
|
||||
// formatDateFromTimestamp,
|
||||
// debug,
|
||||
// info,
|
||||
// error,
|
||||
// warn,
|
||||
// parse2Int,
|
||||
// parse2IntOrNull,
|
||||
// parseToBoolean,
|
||||
// }
|
||||
export {
|
||||
formatDate,
|
||||
formatDateOnly,
|
||||
formatDateFromTimestamp,
|
||||
debug,
|
||||
info,
|
||||
error,
|
||||
warn,
|
||||
parse2Int,
|
||||
parse2IntOrNull,
|
||||
parseToBoolean,
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
import chalk from "chalk"
|
||||
import { formatDate } from "../date"
|
||||
import Logger from "./writter"
|
||||
|
||||
let logger = Logger.getProvider()
|
||||
const logger = console
|
||||
|
||||
const prefixColor = (
|
||||
prefix: string,
|
||||
@ -10,10 +9,6 @@ const prefixColor = (
|
||||
color: string = "green"
|
||||
) => {
|
||||
const timestamp = formatDate(new Date())
|
||||
if (Logger.isEnabled()) {
|
||||
return `[${timestamp}] - ${prefix}: ${message}`
|
||||
}
|
||||
|
||||
const firstStep = chalk`[${timestamp}] - {${color} ${prefix}}:`
|
||||
return `${firstStep} ${message}`
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
import * as fs from "fs"
|
||||
import { Console } from "console"
|
||||
import * as path from "path"
|
||||
import { formatDateOnly } from "../date"
|
||||
|
||||
class Logger {
|
||||
private static enabled: boolean = false
|
||||
public static setEnabled(enabled: boolean): void {
|
||||
this.enabled = enabled
|
||||
}
|
||||
public static isEnabled(): boolean {
|
||||
return this.enabled
|
||||
}
|
||||
|
||||
private static logDir?: string
|
||||
public static setLogDir(logDir: string): void {
|
||||
this.logDir = logDir
|
||||
}
|
||||
public static getLogDir(): string | null | undefined {
|
||||
return this.logDir
|
||||
}
|
||||
|
||||
public static getProvider(): Console {
|
||||
if (this.isEnabled()) {
|
||||
const rootPath = path.resolve(this.getLogDir() || "./")
|
||||
const logPath = `${rootPath}/logs`
|
||||
|
||||
if (!fs.existsSync(logPath)) {
|
||||
fs.mkdirSync(logPath, { recursive: true })
|
||||
}
|
||||
|
||||
const time = formatDateOnly(new Date()).replace(/\//g, "-")
|
||||
|
||||
console.log("Logs writing to path =>", logPath)
|
||||
const output = fs.createWriteStream(
|
||||
`${logPath}/stdout-${time}.log`,
|
||||
{
|
||||
flags: "a",
|
||||
}
|
||||
)
|
||||
const errorOutput = fs.createWriteStream(
|
||||
`${logPath}/stderr-${time}.log`,
|
||||
{
|
||||
flags: "a",
|
||||
}
|
||||
)
|
||||
|
||||
return new Console({ stdout: output, stderr: errorOutput })
|
||||
}
|
||||
|
||||
return console
|
||||
}
|
||||
}
|
||||
|
||||
export default Logger
|
Loading…
Reference in New Issue
Block a user