mirror of
https://github.com/leon-ai/leon.git
synced 2024-09-11 10:25:40 +03:00
refactor(server): log helper to TypeScript
This commit is contained in:
parent
c979a9ea08
commit
b7f20710db
@ -1,6 +1,6 @@
|
||||
import { command } from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Build web app
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import buildApp from './build-app'
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import execa from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import os from '@/helpers/os'
|
||||
|
||||
/**
|
||||
|
@ -5,7 +5,7 @@ import { command } from 'execa'
|
||||
import semver from 'semver'
|
||||
|
||||
import { version } from '@@/package.json'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import { join } from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getSkillDomains } from '@/helpers/skill-domain'
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* This script is executed after "git commit" or "git merge" (Git hook https://git-scm.com/docs/githooks#_commit_msg)
|
||||
|
@ -4,7 +4,7 @@ import fs from 'fs'
|
||||
import { prompt } from 'inquirer'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { randomString } from '@/helpers/string'
|
||||
|
||||
dotenv.config()
|
||||
|
@ -2,7 +2,7 @@ import dotenv from 'dotenv'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import { langs } from '@@/core/langs.json'
|
||||
import { getSkillDomains } from '@/helpers/skill-domain'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import generateHttpApiKey from './generate-http-api-key'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import generateSkillsEndpoints from './generate-skills-endpoints'
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { command } from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import loader from '@/helpers/loader'
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { command } from 'execa'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Update version number in files which need version number
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import loader from '@/helpers/loader'
|
||||
|
||||
import updateVersion from './update-version'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { command } from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Update version number in files which need version number
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import cleanTestDbs from './clean-test-dbs'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import setupHotword from './setup-hotword'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import setupStt from './setup-stt'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import setupTts from './setup-tts'
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { command } from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import os from '@/helpers/os'
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
import loader from '@/helpers/loader'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import checkOs from '../check-os'
|
||||
import setupHotword from './setup-hotword'
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { command } from 'execa'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import os from '@/helpers/os'
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { command } from 'execa'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import os from '@/helpers/os'
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Setup Leon's core configuration
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { prompt } from 'inquirer'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Duplicate the .env.sample to .env file
|
||||
|
@ -2,7 +2,7 @@ import { command } from 'execa'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Download and setup Leon's Python packages dependencies
|
||||
|
@ -2,7 +2,7 @@ import { commandSync } from 'execa'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getSkillDomains } from '@/helpers/skill-domain'
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,5 @@
|
||||
import loader from '@/helpers/loader'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import train from '../train/train'
|
||||
import generateHttpApiKey from '../generate/generate-http-api-key'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { command } from 'execa'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import loader from '@/helpers/loader'
|
||||
|
||||
/**
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
import train from './train'
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Train global entities
|
||||
|
@ -2,7 +2,7 @@ import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { composeFromPattern } from '@nlpjs/utils'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import json from '@/helpers/json'
|
||||
import { findAndMap } from '@/helpers/string'
|
||||
import { getSkillDomains } from '@/helpers/skill-domain'
|
||||
|
@ -2,7 +2,7 @@ import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { composeFromPattern } from '@nlpjs/utils'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
/**
|
||||
* Train global resolvers
|
||||
|
@ -2,7 +2,7 @@ import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { composeFromPattern } from '@nlpjs/utils'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getSkillDomains } from '@/helpers/skill-domain'
|
||||
import json from '@/helpers/json'
|
||||
|
||||
|
@ -3,7 +3,7 @@ import { Nlp } from '@nlpjs/nlp'
|
||||
import { LangAll } from '@nlpjs/lang-all'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getShortLanguages } from '@/helpers/lang'
|
||||
import trainGlobalResolvers from './train-resolvers-model/train-global-resolvers'
|
||||
import trainSkillsResolvers from './train-resolvers-model/train-skills-resolvers'
|
||||
|
@ -2,7 +2,7 @@ import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg'
|
||||
import Ffmpeg from 'fluent-ffmpeg'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
const audios = {
|
||||
webm: `${__dirname}/../tmp/speech.webm`,
|
||||
|
@ -4,7 +4,7 @@ import { spawn } from 'child_process'
|
||||
|
||||
import { langs } from '@@/core/langs.json'
|
||||
import { HAS_TTS } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { findAndMap, randomString } from '@/helpers/string'
|
||||
import Synchronizer from '@/core/synchronizer'
|
||||
import { getLongLanguageCode } from '@/helpers/lang'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
const maxContextHistory = 5
|
||||
const defaultActiveContext = {
|
||||
|
@ -2,7 +2,7 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
import archiver from 'archiver'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { ucFirst } from '@/helpers/string'
|
||||
|
||||
const getDownloads = async (fastify, options) => {
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
STT_PROVIDER,
|
||||
TTS_PROVIDER
|
||||
} from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
const getInfo = async (fastify, options) => {
|
||||
fastify.get(`/api/${options.apiVersion}/info`, (request, reply) => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
const otherMidd = async (request, reply) => {
|
||||
// Disable from the header, else it makes hacker's life easier to know more about our system
|
||||
|
@ -26,7 +26,7 @@ import otherMidd from '@/core/http-server/plugins/other'
|
||||
import keyMidd from '@/core/http-server/plugins/key'
|
||||
import infoPlugin from '@/core/http-server/api/info'
|
||||
import downloadsPlugin from '@/core/http-server/api/downloads'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getDateTime, getTimeZone } from '@/helpers/date'
|
||||
|
||||
const server = {}
|
||||
|
@ -5,7 +5,7 @@
|
||||
*/
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { removeEndPunctuation, snakeToPascalCase } from '@/helpers/string'
|
||||
|
||||
class Ner {
|
||||
|
@ -17,7 +17,7 @@ import {
|
||||
TCP_SERVER_PORT
|
||||
} from '@/constants'
|
||||
import Ner from '@/core/ner'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { ucFirst } from '@/helpers/string'
|
||||
import { getShortLanguages, getLongLanguageCode } from '@/helpers/lang'
|
||||
import TcpClient from '@/core/tcp-client'
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { waterfall } from 'async'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
class Synchronizer {
|
||||
constructor(brain, classification, sync) {
|
||||
|
@ -2,7 +2,7 @@ import Net from 'net'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
import { IS_PRODUCTION_ENV } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
// Time interval between each try (in ms)
|
||||
const INTERVAL = IS_PRODUCTION_ENV ? 3000 : 300
|
||||
|
@ -3,7 +3,7 @@ import utc from 'dayjs/plugin/utc'
|
||||
import timezone from 'dayjs/plugin/timezone'
|
||||
|
||||
import { TIME_ZONE } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Spinner } from 'cli-spinner'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
const sentences = [
|
||||
'This process takes time, please go for a coffee (or a fruit juice)',
|
||||
|
@ -1,42 +0,0 @@
|
||||
import fs from 'fs'
|
||||
|
||||
import { IS_TESTING_ENV } from '@/constants'
|
||||
import { getDateTime } from '@/helpers/date'
|
||||
|
||||
const log = {}
|
||||
|
||||
log.success = (value) => console.log('\x1b[32m✅ %s\x1b[0m', value)
|
||||
|
||||
log.info = (value) => console.info('\x1b[36mℹ️ %s\x1b[0m', value)
|
||||
|
||||
log.error = (value) => {
|
||||
const path = `${__dirname}/../../../logs/errors.log`
|
||||
const errMessage = 'Not able to log the error'
|
||||
const data = `${getDateTime()} - ${value}`
|
||||
|
||||
if (!IS_TESTING_ENV) {
|
||||
/* istanbul ignore next */
|
||||
if (!fs.existsSync(path)) {
|
||||
fs.writeFile(path, data, { flags: 'wx' }, (err) => {
|
||||
if (err) log.warning(errMessage)
|
||||
})
|
||||
} else {
|
||||
fs.appendFile(path, `\n${data}`, (err) => {
|
||||
if (err) log.warning(errMessage)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return console.error('\x1b[31m🚨 %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
log.warning = (value) => console.warn('\x1b[33m⚠️ %s\x1b[0m', value)
|
||||
|
||||
log.debug = (value) => console.info('\u001b[35m🐞 [DEBUG] %s\x1b[0m', value)
|
||||
|
||||
log.title = (value) =>
|
||||
console.log('\n\n\x1b[7m.: %s :.\x1b[0m', value.toUpperCase())
|
||||
|
||||
log.default = (value) => console.log('%s', value)
|
||||
|
||||
export default log
|
69
server/src/helpers/log.ts
Normal file
69
server/src/helpers/log.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { IS_TESTING_ENV } from '@/constants'
|
||||
import { getDateTime } from '@/helpers/date'
|
||||
|
||||
/**
|
||||
* Log Singleton Class.
|
||||
*/
|
||||
export class Log {
|
||||
static readonly ERROR_PATH = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'logs',
|
||||
'errors.log'
|
||||
)
|
||||
private static instance: Log
|
||||
|
||||
private constructor() {
|
||||
// Singleton
|
||||
}
|
||||
|
||||
public static getInstance(): Log {
|
||||
if (Log.instance == null) {
|
||||
Log.instance = new Log()
|
||||
}
|
||||
return Log.instance
|
||||
}
|
||||
|
||||
public success(value: string): void {
|
||||
console.log('\x1b[32m✅ %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
public info(value: string): void {
|
||||
console.info('\x1b[36mℹ️ %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
public warning(value: string): void {
|
||||
console.warn('\x1b[33m⚠️ %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
public debug(value: string): void {
|
||||
console.info('\u001b[35m🐞 [DEBUG] %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
public error(value: string): void {
|
||||
const data = `${getDateTime()} - ${value}`
|
||||
if (!IS_TESTING_ENV) {
|
||||
if (fs.existsSync(Log.ERROR_PATH)) {
|
||||
fs.appendFileSync(Log.ERROR_PATH, `\n${data}`)
|
||||
} else {
|
||||
fs.writeFileSync(Log.ERROR_PATH, data, { flag: 'wx' })
|
||||
}
|
||||
}
|
||||
console.error('\x1b[31m🚨 %s\x1b[0m', value)
|
||||
}
|
||||
|
||||
public title(value: string): void {
|
||||
console.log('\n\n\x1b[7m.: %s :.\x1b[0m', value.toUpperCase())
|
||||
}
|
||||
|
||||
public default(value: string): void {
|
||||
console.log(value)
|
||||
}
|
||||
}
|
||||
|
||||
export const log = Log.getInstance()
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import { Model } from 'stt'
|
||||
|
||||
import { IS_TESTING_ENV } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
log.title('Coqui STT Parser')
|
||||
|
||||
|
@ -2,7 +2,7 @@ import path from 'path'
|
||||
import stt from '@google-cloud/speech'
|
||||
|
||||
import { LANG } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
log.title('Google Cloud STT Parser')
|
||||
|
||||
|
@ -3,7 +3,7 @@ import path from 'path'
|
||||
|
||||
import { IS_TESTING_ENV } from '@/constants'
|
||||
import Asr from '@/core/asr'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
class Stt {
|
||||
constructor(socket, provider) {
|
||||
|
@ -5,7 +5,7 @@ import path from 'path'
|
||||
import { Duplex } from 'stream'
|
||||
|
||||
import { LANG } from '@/constants'
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
log.title('Watson STT Parser')
|
||||
|
||||
|
@ -5,7 +5,7 @@ import { path as ffprobePath } from '@ffprobe-installer/ffprobe'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { randomString } from '@/helpers/string'
|
||||
|
||||
log.title('Amazon Polly Synthesizer')
|
||||
|
@ -4,7 +4,7 @@ import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg'
|
||||
import { path as ffprobePath } from '@ffprobe-installer/ffprobe'
|
||||
import fs from 'fs'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { randomString } from '@/helpers/string'
|
||||
|
||||
log.title('Flite Synthesizer')
|
||||
|
@ -5,7 +5,7 @@ import { path as ffprobePath } from '@ffprobe-installer/ffprobe'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { randomString } from '@/helpers/string'
|
||||
|
||||
log.title('Google Cloud TTS Synthesizer')
|
||||
|
@ -2,7 +2,7 @@ import events from 'events'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { getLongLanguageCode } from '@/helpers/lang'
|
||||
|
||||
class Tts {
|
||||
|
@ -6,7 +6,7 @@ import { path as ffprobePath } from '@ffprobe-installer/ffprobe'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
import { randomString } from '@/helpers/string'
|
||||
|
||||
log.title('Watson TTS Synthesizer')
|
||||
|
@ -1,4 +1,4 @@
|
||||
import log from '@/helpers/log'
|
||||
import { log } from '@/helpers/log'
|
||||
|
||||
describe('log helper', () => {
|
||||
describe('success()', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user