Skip to content

Commit

Permalink
rework logger
Browse files Browse the repository at this point in the history
  • Loading branch information
inetol committed Oct 20, 2024
1 parent 7529241 commit b397e09
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## SERVER:
# Set log verbosity [2]:integer
# (0=error <- 1=warn <- 2=info <- 3=debug)
#LOGLEVEL=2
# Set log verbosity [3]:integer
# (0=none <- 1=error <- 2=warn <- 3=info <- 4=debug)
#LOGLEVEL=3

# Port for the server [4000]:integer
#PORT=4000
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
"@scalar/hono-api-reference": "~0.5.155",
"@types/bun": "~1.1.11",
"@types/node": "~22.7.7",
"chalk": "~5.3.0",
"env-var": "~7.5.0",
"hono": "~4.6.5",
"loglevel": "~1.9.2",
"typescript": "~5.6.3"
},
"devDependencies": {
Expand Down
44 changes: 0 additions & 44 deletions src/logger.ts

This file was deleted.

14 changes: 6 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { LogLevels, logger } from '@x-util/logger.ts';
import { serve } from 'bun';
import { get as envvar } from 'env-var';
import { cors } from 'hono/cors';
import { HTTPException } from 'hono/http-exception';
import { logger } from './logger.ts';
import { documentation } from './server/documentation.ts';
import { endpoints } from './server/endpoints.ts';
import { errorHandler } from './server/errorHandler.ts';
import { ErrorCode } from './types/ErrorHandler.ts';

export const env = {
port: envvar('PORT').default(4000).asPortNumber(),
logLevel: envvar('LOGLEVEL').default(2).asIntPositive(),
logLevel: envvar('LOGLEVEL').default(LogLevels.info).asIntPositive(),
tls: envvar('TLS').asBoolStrict() ?? true,
documentMaxSize: envvar('DOCUMENT_MAXSIZE').default(1024).asIntPositive(),
docsEnabled: envvar('DOCS_ENABLED').asBoolStrict() ?? false,
Expand All @@ -27,12 +27,6 @@ export const config = {
documentNameLengthDefault: 8
} as const;

logger.set(env.logLevel);

process.on('SIGTERM', async () => {
await backend.stop();
});

const instance = new OpenAPIHono().basePath(config.apiPath);

export const server = (): typeof instance => {
Expand Down Expand Up @@ -66,3 +60,7 @@ const backend = serve({
port: env.port,
fetch: server().fetch
});

process.on('SIGTERM', async () => {
await backend.stop();
});
22 changes: 22 additions & 0 deletions src/utils/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type ColorInput, color as bunColor } from 'bun';

const colorString =
(color: ColorInput) =>
(...text: unknown[]): string => {
return bunColor(color, 'ansi') + text.join(' ') + colors.reset;
};

export const colors = {
red: colorString('#ef5454'),
orange: colorString('#ef8354'),
yellow: colorString('#efd554'),
green: colorString('#70ef54'),
turquoise: colorString('#54efef'),
blue: colorString('#5954ef'),
purple: colorString('#a454ef'),
pink: colorString('#ef54d5'),
gray: colorString('#888'),
black: colorString('#000'),
white: colorString('#fff'),
reset: '\x1b[0m'
} as const;
41 changes: 41 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { colors } from '@x-util/colors.ts';

export enum LogLevels {
none = 0,
error = 1,
warn = 2,
info = 3,
debug = 4
}

let logLevel: LogLevels = LogLevels.info;

export const logger = {
set: (level: LogLevels): void => {
logLevel = level;
},

error: (...text: unknown[]): void => {
if (logLevel >= LogLevels.error) {
console.error(colors.gray('[BACKEND]'), colors.red('[ERROR]'), text.join('\n'));
}
},

warn: (...text: unknown[]): void => {
if (logLevel >= LogLevels.warn) {
console.warn(colors.gray('[BACKEND]'), colors.yellow('[WARN]'), text.join('\n'));
}
},

info: (...text: unknown[]): void => {
if (logLevel >= LogLevels.info) {
console.info(colors.gray('[BACKEND]'), colors.blue('[INFO]'), text.join('\n'));
}
},

debug: (...text: unknown[]): void => {
if (logLevel >= LogLevels.debug) {
console.debug(colors.gray('[BACKEND]'), colors.gray('[DEBUG]'), text.join('\n'));
}
}
} as const;

0 comments on commit b397e09

Please sign in to comment.