Skip to content

Commit d80ec5d

Browse files
authored
Merge pull request #3 from truehazker/feature/logger-improvement
feat: 🎨 improve logging and error handling
2 parents fd294dc + 7da2aad commit d80ec5d

File tree

6 files changed

+73
-25
lines changed

6 files changed

+73
-25
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Changed
11+
12+
- Updated logger integration to use plugin-based approach at root app level
13+
- Improved app initialization and launch process with proper signal handling (SIGINT/SIGTERM)
14+
- Enhanced error logging with structured HTTP request context
15+
- Improved error handling in the users module
16+
817
## [0.2.4] - 2025-11-20
918

1019
### Fixed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ src/
134134
└── main.ts # Application entry point
135135
```
136136

137+
### Important Notes on Logger Usage
138+
139+
The logger is initialized in the root application (`src/main.ts`) using `.use(log.into({...}))`. To avoid duplicate logs, submodules should import and use the logger directly from `src/common/logger` rather than relying on Elysia's context. Using the logger from context in submodules will result in duplicate log entries.
140+
141+
**Example:**
142+
143+
```typescript
144+
// ✅ Correct: Import logger directly
145+
import { log } from 'src/common/logger';
146+
147+
// ❌ Incorrect: Using logger from Elysia context in submodules
148+
// This will cause duplicate logs
149+
```
150+
137151
## Configuration
138152

139153
The application uses [Envalid](https://github.com/af/envalid) for type-safe environment variable validation. All configuration is centralized in `src/common/config.ts`.

bun.lock

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"elysia": "^1.4.16",
1414
"envalid": "^8.1.1",
1515
"pg": "^8.16.3",
16+
"pino": "^10.1.0",
1617
},
1718
"devDependencies": {
1819
"@biomejs/biome": "2.3.6",
@@ -115,24 +116,20 @@
115116

116117
"@tokenizer/token": ["@tokenizer/[email protected]", "", {}, "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A=="],
117118

118-
"@types/bun": ["@types/[email protected].2", "", { "dependencies": { "bun-types": "1.3.2" } }, "sha512-t15P7k5UIgHKkxwnMNkJbWlh/617rkDGEdSsDbu+qNHTaz9SKf7aC8fiIlUdD5RPpH6GEkP0cK7WlvmrEBRtWg=="],
119+
"@types/bun": ["@types/[email protected].3", "", { "dependencies": { "bun-types": "1.3.3" } }, "sha512-ogrKbJ2X5N0kWLLFKeytG0eHDleBYtngtlbu9cyBKFtNL3cnpDZkNdQj8flVf6WTZUX5ulI9AY1oa7ljhSrp+g=="],
119120

120121
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="],
121122

122-
"@types/react": ["@types/[email protected]", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-p/jUvulfgU7oKtj6Xpk8cA2Y1xKTtICGpJYeJXz2YVO2UcvjQgeRMLDGfDeqeRW2Ta+0QNFwcc8X3GH8SxZz6w=="],
123-
124123
"atomic-sleep": ["[email protected]", "", {}, "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ=="],
125124

126125
"buffer-from": ["[email protected]", "", {}, "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ=="],
127126

128-
"bun-types": ["[email protected].2", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg=="],
127+
"bun-types": ["[email protected].3", "", { "dependencies": { "@types/node": "*" } }, "sha512-z3Xwlg7j2l9JY27x5Qn3Wlyos8YAp0kKRlrePAOjgjMGS5IG6E7Jnlx736vH9UVI4wUICwwhC9anYL++XeOgTQ=="],
129128

130129
"colorette": ["[email protected]", "", {}, "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w=="],
131130

132131
"cookie": ["[email protected]", "", {}, "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA=="],
133132

134-
"csstype": ["[email protected]", "", {}, "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ=="],
135-
136133
"dateformat": ["[email protected]", "", {}, "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA=="],
137134

138135
"debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="],

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
"drizzle-typebox": "^0.3.3",
2222
"elysia": "^1.4.16",
2323
"envalid": "^8.1.1",
24-
"pg": "^8.16.3"
24+
"pg": "^8.16.3",
25+
"pino": "^10.1.0"
2526
},
2627
"devDependencies": {
2728
"@biomejs/biome": "2.3.6",
28-
"@types/bun": "^1.3.2",
29+
"@types/bun": "^1.3.3",
2930
"drizzle-kit": "^0.31.7",
3031
"pino-pretty": "^13.1.2"
3132
}

src/main.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ const app = new Elysia()
1414
autoLogging: false,
1515
}),
1616
)
17-
.onError((ctx) => {
18-
log.error(ctx.error);
17+
.onError(({ code, error, request }) => {
18+
log.error(
19+
{
20+
code,
21+
err: error,
22+
http: request
23+
? {
24+
method: request.method,
25+
url: request.url,
26+
referrer: request.headers.get('referer') ?? undefined,
27+
}
28+
: undefined,
29+
},
30+
'Unhandled request error',
31+
);
32+
return error;
1933
})
2034
.use(
2135
openapi({
@@ -34,12 +48,13 @@ const app = new Elysia()
3448
},
3549
}),
3650
)
37-
.use(users)
38-
.listen(config.SERVER_PORT, ({ development, hostname, port }) => {
39-
log.info(
40-
`🦊 Elysia is running at ${hostname}:${port} ${development ? '🚧 in development mode!🚧' : ''}`,
41-
);
42-
});
51+
.use(users);
52+
53+
app.listen(config.SERVER_PORT, ({ development, hostname, port }) => {
54+
log.info(
55+
`🦊 Elysia is running at ${hostname}:${port} ${development ? '🚧 in development mode!🚧' : ''}`,
56+
);
57+
});
4358

4459
process.once('SIGINT', () => {
4560
log.info('SIGINT received, shutting down...');

src/modules/users/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { Elysia } from 'elysia';
2-
import { log } from '../../common/logger';
1+
import { Elysia, status } from 'elysia';
2+
import { log } from 'src/common/logger';
33
import { type UsersModel, usersModelPlugin } from './model';
44
import { UsersService } from './service';
55

66
export const users = new Elysia({ prefix: '/users', tags: ['Users'] })
7-
.use(log.into())
87
.use(usersModelPlugin)
98
.post(
109
'/',
11-
async ({ log, body }): Promise<UsersModel.createResponse> => {
12-
const user = await UsersService.create(body);
13-
log.info(`Created user ${user.name}`);
14-
return user;
10+
async ({ body }): Promise<UsersModel.createResponse> => {
11+
try {
12+
const user = await UsersService.create(body);
13+
log.info(`Created user ${user.name}`);
14+
return user;
15+
} catch {
16+
throw status(422, {
17+
message: 'Failed to create user' satisfies UsersModel.createError,
18+
});
19+
}
1520
},
1621
{
1722
body: 'users.createRequest',
@@ -28,8 +33,15 @@ export const users = new Elysia({ prefix: '/users', tags: ['Users'] })
2833
.get(
2934
'/',
3035
async ({ query }): Promise<UsersModel.getResponse> => {
31-
const users = await UsersService.get(query);
32-
return users;
36+
try {
37+
const users = await UsersService.get(query);
38+
log.info(`Got users ${users.total}`);
39+
return users;
40+
} catch {
41+
throw status(422, {
42+
message: 'Failed to get users' satisfies UsersModel.getError,
43+
});
44+
}
3345
},
3446
{
3547
query: 'users.getQuery',

0 commit comments

Comments
 (0)