jan/core/src/node/log.ts
NamH b2ff76ce80
fix: app log not being printed (#1790)
Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
2024-01-25 22:29:27 +07:00

38 lines
889 B
TypeScript

import fs from 'fs'
import util from 'util'
import { getAppLogPath, getServerLogPath } from './utils'
export const log = (message: string) => {
const path = getAppLogPath()
if (!message.startsWith('[')) {
message = `[APP]::${message}`
}
message = `${new Date().toISOString()} ${message}`
writeLog(message, path)
}
export const logServer = (message: string) => {
const path = getServerLogPath()
if (!message.startsWith('[')) {
message = `[SERVER]::${message}`
}
message = `${new Date().toISOString()} ${message}`
writeLog(message, path)
}
const writeLog = (message: string, logPath: string) => {
if (!fs.existsSync(logPath)) {
fs.writeFileSync(logPath, message)
} else {
const logFile = fs.createWriteStream(logPath, {
flags: 'a',
})
logFile.write(util.format(message) + '\n')
logFile.close()
console.debug(message)
}
}