Skip to content

Commit

Permalink
Merge pull request #1 from OXeu/feat_logger
Browse files Browse the repository at this point in the history
feat: add utils/logger 🧀
  • Loading branch information
OXeu authored May 22, 2024
2 parents 7ac5fe2 + c453fe1 commit dc73914
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@ dist

# Finder (MacOS) folder config
.DS_Store
bunfig.toml
3 changes: 2 additions & 1 deletion src/db/db.ts
Original file line number Diff line number Diff line change
@@ -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<typeof schema>;

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 });
}
Expand Down
3 changes: 2 additions & 1 deletion src/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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`);
}
11 changes: 6 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* @Author: Bin
* @Date: 2024-05-22
* @FilePath: /oXeu_rin/src/utils/index.ts
*/

export * from './logger'
11 changes: 11 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit dc73914

Please sign in to comment.