Skip to content

Commit dc73914

Browse files
authored
Merge pull request #1 from OXeu/feat_logger
feat: add utils/logger 🧀
2 parents 7ac5fe2 + c453fe1 commit dc73914

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,4 @@ dist
178178

179179
# Finder (MacOS) folder config
180180
.DS_Store
181+
bunfig.toml

src/db/db.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Database } from "bun:sqlite";
22
import { drizzle, type BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
33
import * as schema from './schema';
4+
import {logPlugin, logger} from '../utils/logger'
45

56
let dbInstance: BunSQLiteDatabase<typeof schema>;
67

78
const getDbInstance = () => {
89
if (!dbInstance) {
9-
console.log(`Connect to ${process.env.DB_PATH}`);
10+
logger.debug(`Connect to ${process.env.DB_PATH}`);
1011
const connection = new Database(process.env.DB_PATH ?? 'sqlite.db', { create: true });
1112
dbInstance = drizzle(connection, { schema: schema });
1213
}

src/db/migrate.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Database } from "bun:sqlite";
22
import { drizzle } from "drizzle-orm/bun-sqlite";
33
import { migrate } from 'drizzle-orm/bun-sqlite/migrator';
4+
import { logger} from '../utils/logger'
45

56
export async function migration() {
67
const start = new Date().getTime();
@@ -11,5 +12,5 @@ export async function migration() {
1112
await connection.close();
1213

1314
const end = new Date().getTime();
14-
console.log(`Migration to ${db_path} took ${end - start}ms`);
15+
logger.debug(`Migration to ${db_path} took ${end - start}ms`);
1516
}

src/server.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import cors from '@elysiajs/cors';
22
import { Elysia } from 'elysia';
3-
import { Logestic } from 'logestic';
43
import { PostService } from './action/post';
54
import { UserService } from './action/user';
65
import { migration } from './db/migrate';
6+
import {logPlugin, logger} from './utils/logger'
77

88
migration()
99

10-
const log = Logestic.preset('fancy')
1110
export const app = new Elysia()
1211
.use(cors())
13-
.use(log)
12+
.use(logPlugin)
1413
.use(UserService)
1514
.use(PostService)
16-
.get('/', ({uid}) => `Hi ${uid}`)
15+
.get('/', ({ uid }) => `Hi ${uid}`)
1716
.onError(({ code }) => {
1817
if (code === 'NOT_FOUND')
1918
return ':('
2019
})
21-
.listen(process.env.PORT ?? 3001)
20+
.listen(process.env.PORT ?? 3001, () => {
21+
logger.info(`[Rim] Server is running: http://localhost:${process.env.PORT ?? 3001}`)
22+
})
2223

2324
export type App = typeof app

src/utils/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* @Author: Bin
3+
* @Date: 2024-05-22
4+
* @FilePath: /oXeu_rin/src/utils/index.ts
5+
*/
6+
7+
export * from './logger'

src/utils/logger.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: Bin
3+
* @Date: 2024-05-22
4+
* @FilePath: /oXeu_rin/src/utils/logger.ts
5+
*/
6+
import { Logestic } from 'logestic';
7+
8+
const logPlugin = Logestic.preset('fancy')
9+
const {logestic: logger} = logPlugin.decorator;
10+
11+
export {logPlugin, logger}

0 commit comments

Comments
 (0)