Skip to content

Commit 02dff79

Browse files
committed
refactor(backend): use console logger directly for better cloud compat
1 parent cc52610 commit 02dff79

9 files changed

Lines changed: 25 additions & 24 deletions

File tree

apps/backend/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Things like 3rd party APIs, DBs, Storages connectors, etc, should be placed in `
1818

1919
Things that interact with `#src/providers` should be placed in `#src/services` folder. (like an `user` service)
2020

21-
Other globally reuseable code should be placed in `#src/helpers` folder. (like `error`, `logger` utils)
21+
Other globally reuseable code should be placed in `#src/helpers` folder. (like `error`, `i18n` utils)
2222

2323
Locally reusable code should be placed in the same folder as the file that uses it, its name should be its usable scope, suffixing the file name with `.helper`, e.g: `/api/dummy/hello.helper.ts`, `/api/$.helper.ts`.
2424

@@ -38,3 +38,9 @@ Again, the recommended structure is to mirror the api url path, but, if for some
3838
#### Hono `app` export naming conventions:
3939
* Root and main app entries (app.ts, $.ts) should be named as: `<Name>App`, and it should only `.route` other instances or `.use` middlewares, do not define routes on the `App` instance.
4040
* For other app entries, a.k.a routes defines, it should be named as: `<Name>RouteApp`, i.e: `/api/dummy/hello.ts` should be named as `dummyHelloRouteApp`/`helloRouteApp`.
41+
42+
## Notes & Guides
43+
44+
### Deploying to AWS Lambda for medium / big functions
45+
46+
When deploying to AWS Lambda, the best pratice is that you'll use something like `https://github.com/0x80/isolate-package` to isolate and create a zip file for all prod dependencies packages, zip it, and deploy it as a "layer" for the Lambda function.

apps/backend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"@vitest/coverage-v8": "^3.2.4",
3131
"arktype": "^2.1.20",
3232
"backend-convex": "workspace:*",
33-
"consola": "^3.4.2",
3433
"convex": "^1.24.8",
3534
"hono": "^4.8.0",
3635
"hono-adapter-aws-lambda": "^1.3.3",

apps/backend/src/app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { cors } from 'hono/cors'
55
import { logger as loggerMiddleware } from 'hono/logger'
66
import { env, isWorkerd } from 'std-env'
77
import { apiApp } from './api/$'
8-
import { logger } from './helpers/logger'
98
import { setupOpenAPI } from './openAPI'
109
import { providersInit } from './providers'
1110

@@ -28,7 +27,7 @@ export const app = appFactory.createApp()
2827
.onError(errorHandler)
2928

3029
// Request logging middleware
31-
.use(loggerMiddleware(logger.log))
30+
.use(loggerMiddleware())
3231

3332
// Register trigger routes, after the logging middleware but before the request-based middlewares
3433
.route('/', triggerFactory.honoApp)

apps/backend/src/helpers/error.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
/* eslint-disable no-console */
12
import type { HonoEnv } from '#src/types.js'
23
import type { ErrorHandler } from 'hono'
34
import type { ContentfulStatusCode } from 'hono/utils/http-status'
4-
import { logger } from '#src/helpers/logger.js'
55
import { DetailedError } from '@namesmt/utils'
66
import { HTTPException } from 'hono/http-exception'
77

88
export const errorHandler: ErrorHandler<HonoEnv> = (err, c) => {
9-
logger.error(err)
9+
const _e = err as any
10+
// If Error is not server exception, log to debug only
11+
if (
12+
(_e.statusCode && _e.statusCode < 500)
13+
|| (_e.status && _e.status < 500)
14+
) {
15+
console.debug(err)
16+
}
17+
else {
18+
console.error(err)
19+
}
1020

1121
// Handling of default Hono's HTTPException
1222
if (err instanceof HTTPException) {

apps/backend/src/helpers/logger.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

apps/backend/src/providers/auth/kinde-main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { logger } from '#src/helpers/logger.js'
21
import { createKindeServerClient, GrantType } from '@kinde-oss/kinde-typescript-sdk'
32
import { env } from 'std-env'
43
import { cacheProvider, getCachedProvider, isNonSharingPlatforms } from '..'
@@ -20,7 +19,7 @@ export async function initKindeClient() {
2019
const cachedClient = getCachedKindeClient()
2120

2221
if (!isNonSharingPlatforms && cachedClient)
23-
logger.warn('Already initialized')
22+
console.warn('Already initialized')
2423

2524
const client = createKindeServerClient(GrantType.AUTHORIZATION_CODE, {
2625
authDomain: env.KINDE_DOMAIN!,

apps/backend/src/providers/baas/convex-main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { logger } from '#src/helpers/logger.js'
21
import { ConvexHttpClient } from 'convex/browser'
32
import { env } from 'std-env'
43
import { cacheProvider, getCachedProvider, isNonSharingPlatforms } from '..'
@@ -20,7 +19,7 @@ export async function initConvexClient() {
2019
const cachedClient = getCachedConvexClient()
2120

2221
if (!isNonSharingPlatforms && cachedClient)
23-
logger.warn('Already initialized')
22+
console.warn('Already initialized')
2423

2524
if (!env.CONVEX_URL)
2625
throw new Error('CONVEX_URL is not set')

apps/backend/test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { logger } from '#src/helpers/logger.js'
21
import { expect, it } from 'vitest'
32

4-
it('logger', () => {
5-
expect(logger).toHaveProperty('info')
3+
it('hello', () => {
4+
const hello = 'world'
5+
expect(hello).toBe('world')
66
})

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)