Skip to content

Commit

Permalink
feat(*): add log on start
Browse files Browse the repository at this point in the history
  • Loading branch information
PunGrumpy committed Nov 7, 2023
1 parent f1d9583 commit f1adeb9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
Binary file modified .github/images/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ const app = new Elysia({
name: 'Logixlysia Example'
}).use(logger())

console.log(
`πŸ§ͺ Listening on http://${app.server!.hostname}:${app.server!.port}`
)
app.listen(3000)
```

## `πŸ“„` License
Expand Down
5 changes: 1 addition & 4 deletions example/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@ const app = new Elysia({
message: 'Basic Example'
}
})
.listen(3000)

console.log(
`πŸ§ͺ Listening on http://${app.server!.hostname}:${app.server!.port}`
)
app.listen(3000)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logixlysia",
"version": "1.0.1",
"version": "1.1.0",
"description": "🦊 Logixlysia is a logger for Elysia",
"type": "module",
"module": "src/index.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Elysia from 'elysia'
import { formatLogger } from './logger'
import { startString } from './utils/start'
import { Server } from 'bun'

export const logger = () => {
const log = formatLogger()

const elysia = new Elysia({
name: 'Logixlysia'
})
.onStart(ctx => {
startString(ctx.app.server as Server)
})
.onRequest(ctx => {
ctx.store = { beforeTime: process.hrtime.bigint() } as {
beforeTime: bigint
Expand Down
34 changes: 34 additions & 0 deletions src/utils/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interface Server {
hostname?: string
port?: number
protocol?: string
}

function createBoxText(text: string, width: number): string {
const padding = ' '.repeat((width - text.length) / 2)
return `${padding}${text}${padding}`
}

function startString(config: Server): void {
const { hostname, port, protocol } = config
const ELYSIA_VERSION = import.meta.require('elysia/package.json').version
const title = `Elysia v${ELYSIA_VERSION}`
const message = `🦊 Elysia is running at ${protocol}://${hostname}:${port}`
const messageWidth = message.length
const boxWidth = Math.max(title.length, messageWidth) + 4
const border = '─'.repeat(boxWidth)

process.stdout.write('\x1Bc')

console.log(`
β”Œ${border}┐
β”‚${createBoxText('', boxWidth)} β”‚
β”‚${createBoxText(title, boxWidth)} β”‚
β”‚${createBoxText('', boxWidth)} β”‚
β”‚${createBoxText(message, boxWidth)}β”‚
β”‚${createBoxText('', boxWidth)} β”‚
β””${border}β”˜
`)
}

export { Server, startString }

0 comments on commit f1adeb9

Please sign in to comment.