diff --git a/.gitignore b/.gitignore index aa60a05c..e34567c1 100644 --- a/.gitignore +++ b/.gitignore @@ -178,3 +178,4 @@ dist # Finder (MacOS) folder config .DS_Store +bunfig.toml diff --git a/src/db/db.ts b/src/db/db.ts index 5c1a5b1f..0424b3a6 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -1,12 +1,13 @@ import { Database } from "bun:sqlite"; import { drizzle, type BunSQLiteDatabase } from "drizzle-orm/bun-sqlite"; import * as schema from './schema'; +import {logPlugin, logger} from '../utils/logger' let dbInstance: BunSQLiteDatabase; const getDbInstance = () => { if (!dbInstance) { - console.log(`Connect to ${process.env.DB_PATH}`); + logger.debug(`Connect to ${process.env.DB_PATH}`); const connection = new Database(process.env.DB_PATH ?? 'sqlite.db', { create: true }); dbInstance = drizzle(connection, { schema: schema }); } diff --git a/src/db/migrate.ts b/src/db/migrate.ts index 642226c6..27d20729 100644 --- a/src/db/migrate.ts +++ b/src/db/migrate.ts @@ -1,6 +1,7 @@ import { Database } from "bun:sqlite"; import { drizzle } from "drizzle-orm/bun-sqlite"; import { migrate } from 'drizzle-orm/bun-sqlite/migrator'; +import { logger} from '../utils/logger' export async function migration() { const start = new Date().getTime(); @@ -11,5 +12,5 @@ export async function migration() { await connection.close(); const end = new Date().getTime(); - console.log(`Migration to ${db_path} took ${end - start}ms`); + logger.debug(`Migration to ${db_path} took ${end - start}ms`); } diff --git a/src/server.ts b/src/server.ts index 5d13c009..8a6cbf6d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,23 +1,24 @@ import cors from '@elysiajs/cors'; import { Elysia } from 'elysia'; -import { Logestic } from 'logestic'; import { PostService } from './action/post'; import { UserService } from './action/user'; import { migration } from './db/migrate'; +import {logPlugin, logger} from './utils/logger' migration() -const log = Logestic.preset('fancy') export const app = new Elysia() .use(cors()) - .use(log) + .use(logPlugin) .use(UserService) .use(PostService) - .get('/', ({uid}) => `Hi ${uid}`) + .get('/', ({ uid }) => `Hi ${uid}`) .onError(({ code }) => { if (code === 'NOT_FOUND') return ':(' }) - .listen(process.env.PORT ?? 3001) + .listen(process.env.PORT ?? 3001, () => { + logger.info(`[Rim] Server is running: http://localhost:${process.env.PORT ?? 3001}`) + }) export type App = typeof app \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..dd0b4f78 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,7 @@ +/* + * @Author: Bin + * @Date: 2024-05-22 + * @FilePath: /oXeu_rin/src/utils/index.ts + */ + +export * from './logger' diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 00000000..516a7247 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,11 @@ +/* + * @Author: Bin + * @Date: 2024-05-22 + * @FilePath: /oXeu_rin/src/utils/logger.ts + */ +import { Logestic } from 'logestic'; + +const logPlugin = Logestic.preset('fancy') +const {logestic: logger} = logPlugin.decorator; + +export {logPlugin, logger} \ No newline at end of file