131 lines
3.3 KiB
TypeScript
Executable File
131 lines
3.3 KiB
TypeScript
Executable File
#!/usr/bin/env bun
|
|
import { pino, type LoggerOptions, type Level } from 'pino';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import chalk from 'chalk';
|
|
import { customLevels } from './pino-pretty-transport';
|
|
import _ from 'lodash'
|
|
|
|
export const jjLoggerObj = {}
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const logFilePath = `${process.env.JJ_SOURCE_DIR}/~~tmp/jj.log`;
|
|
jjLoggerObj['logFilePath'] = logFilePath
|
|
|
|
|
|
|
|
export const jjLogger = pino({
|
|
customLevels: customLevels,
|
|
level: 'info',
|
|
base: {
|
|
host: os.hostname(),
|
|
filename: path.basename(__filename),
|
|
app: 'jj',
|
|
},
|
|
transport: {
|
|
targets: [
|
|
{
|
|
target: 'pino/file',
|
|
options: { destination: logFilePath, mkdir: true },
|
|
level: 'info',
|
|
},
|
|
{
|
|
target: './pino-pretty-transport',
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
});
|
|
|
|
jjLoggerObj['jjLogger'] = jjLogger
|
|
|
|
|
|
export const makeJJlogger = (opt) => {
|
|
const childJJLogger = jjLogger.child({
|
|
...opt
|
|
})
|
|
|
|
return childJJLogger
|
|
}
|
|
jjLoggerObj['makeJJlogger'] = makeJJlogger
|
|
|
|
export const jjLog = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.log(message);
|
|
}
|
|
jjLoggerObj['jjLog'] = jjLog
|
|
// jjLog('jjLog', { test1: 'test1' })
|
|
|
|
export const jjLogInfo = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.info(message, opt);
|
|
}
|
|
jjLoggerObj['jjLogInfo'] = jjLogInfo
|
|
// jjLogInfo('jjLogInfo', { test1: 'test1' })
|
|
|
|
export const jjLogHeading = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.heading(`=======================================================
|
|
${message}
|
|
========================================================`, opt);
|
|
}
|
|
jjLoggerObj['jjLogHeading'] = jjLogHeading
|
|
// jjLogHeading('jjLogHeading', { test1: 'test1' })
|
|
|
|
export const jjLogAction = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.action(`${message}`, opt);
|
|
}
|
|
jjLoggerObj['jjLogAction'] = jjLogAction
|
|
// jjLogAction('jjLogAction', { test1: 'test1' })
|
|
export const jjLogError = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.error(message, opt);
|
|
}
|
|
jjLoggerObj['jjLogError'] = jjLogError
|
|
// jjLogError('jjLogError')
|
|
|
|
export const jjLogWarning = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.warn(message, opt);
|
|
}
|
|
jjLoggerObj['jjLogWarning'] = jjLogWarning
|
|
// jjLogWarning('jjLogWarning', { test1: 'test1' })
|
|
|
|
export const jjLogSuccess = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.success(message, opt);
|
|
}
|
|
jjLoggerObj['jjLogSuccess'] = jjLogSuccess
|
|
// jjLogSuccess('jjLogSuccess', { test1: 'test1' })
|
|
|
|
export const jjBgRed = (message, opt = {}) => {
|
|
const cLogger = makeJJlogger(opt)
|
|
cLogger.bgRed(message, opt);
|
|
}
|
|
jjLoggerObj['jjBgRed'] = jjBgRed
|
|
// jjBgRed('jjBgRed', { test1: 'test1' })
|
|
|
|
|
|
|
|
|
|
|
|
if (_.get(process.argv, "[1]", "")?.includes('helpers/jjLogger.ts')) {
|
|
const args = [...process.argv]
|
|
args.shift()
|
|
args.shift()
|
|
if (args.length > 0) {
|
|
const loggerCal = jjLoggerObj[args[0]]
|
|
args.shift()
|
|
const [, ...restArgs] = [, ...args]
|
|
const message = restArgs[0]
|
|
if(!message){
|
|
process.exit(0)
|
|
}
|
|
restArgs.shift()
|
|
loggerCal(message, { ...restArgs })
|
|
}
|
|
} |