-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added log4js logging * Logger as functions * Change default logging level to info. RPC Response has `info` level * Formatting --------- Co-authored-by: shuse2 <[email protected]>
- Loading branch information
Showing
7 changed files
with
185 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import dotenv from 'dotenv'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import log4js from 'log4js'; | ||
import { | ||
JSONRPCRequest, | ||
JSONRPCServerMiddlewareNext, | ||
createJSONRPCErrorResponse, | ||
JSONRPCResponse, | ||
JSONRPCErrorCode, | ||
} from 'json-rpc-2.0'; | ||
import { ErrorCode } from './utils/error'; | ||
dotenv.config(); | ||
|
||
const logger = log4js.getLogger(); | ||
logger.level = process.env.BACKEND_LOGGER_LEVEL ?? 'info'; | ||
|
||
export const TRUNCATE_LENGTH = 150; | ||
|
||
export function truncate(text: string): string { | ||
if (text.length > TRUNCATE_LENGTH) { | ||
return text.substring(0, TRUNCATE_LENGTH) + ' ...'; | ||
} | ||
return text; | ||
} | ||
|
||
export function formatRequest(request: JSONRPCRequest): string { | ||
const method = request.method ?? 'EMPTY_METHOD'; | ||
const params = request.params ?? {}; | ||
|
||
return `${method}:${truncate(JSON.stringify(params))}`; | ||
} | ||
|
||
function expressLog(req: Request, res: Response, next: NextFunction, error: Error | null = null) { | ||
const logger = log4js.getLogger('HTTP'); | ||
const header = | ||
':remote-addr - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent" :response-time'; | ||
|
||
let level = 'debug'; | ||
let getMessage = (req: Request) => `${header} "${truncate(JSON.stringify(req.body))}"`; | ||
if (error) { | ||
if (error.message) { | ||
level = 'warn'; | ||
getMessage = () => `${header} "${error.message}"`; | ||
} else { | ||
logger.error(`Error Occurred: ${JSON.stringify(error)}`); | ||
} | ||
} | ||
|
||
return log4js.connectLogger(logger, { | ||
level, | ||
format: (req: Request, _res: Response, formatter: (str: string) => string) => { | ||
return formatter(getMessage(req)); | ||
}, | ||
})(req, res, next); | ||
} | ||
|
||
export function expressLogger(req: Request, res: Response, next: NextFunction) { | ||
return expressLog(req, res, next); | ||
} | ||
|
||
export function expressErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) { | ||
return expressLog(req, res, next, error); | ||
} | ||
|
||
export async function rpcLogger<ServerParams>( | ||
next: JSONRPCServerMiddlewareNext<ServerParams>, | ||
request: JSONRPCRequest, | ||
serverParams: ServerParams, | ||
) { | ||
const logger = log4js.getLogger('RPC'); | ||
return next(request, serverParams).then((response: JSONRPCResponse | null) => { | ||
if (!response) { | ||
return response; | ||
} | ||
|
||
const message = `Request ${formatRequest(request)} | Response ${truncate(JSON.stringify(response))}`; | ||
if (response.error) { | ||
logger.warn(message); | ||
} else { | ||
logger.info(message); | ||
} | ||
return response; | ||
}); | ||
} | ||
|
||
export async function rpcErrorHandler<ServerParams>( | ||
next: JSONRPCServerMiddlewareNext<ServerParams>, | ||
request: JSONRPCRequest, | ||
serverParams: ServerParams, | ||
) { | ||
try { | ||
return await next(request, serverParams); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
if (Object.values(ErrorCode).includes(error.message as ErrorCode)) { | ||
return createJSONRPCErrorResponse( | ||
request.id ?? null, | ||
JSONRPCErrorCode.InvalidRequest, | ||
error.message, | ||
); | ||
} | ||
// Other RPC Error has been handled by library | ||
return createJSONRPCErrorResponse( | ||
request.id ?? null, | ||
JSONRPCErrorCode.InternalError, | ||
error.message, | ||
); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export default logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2962,6 +2962,11 @@ data-view-byte-offset@^1.0.0: | |
es-errors "^1.3.0" | ||
is-data-view "^1.0.1" | ||
|
||
date-format@^4.0.14: | ||
version "4.0.14" | ||
resolved "https://registry.yarnpkg.com/date-format/-/date-format-4.0.14.tgz#7a8e584434fb169a521c8b7aa481f355810d9400" | ||
integrity sha512-39BOQLs9ZjKh0/patS9nrT8wc3ioX3/eA/zgbKNopnF2wCqJEoxywwwElATYvRsXdnOxA/OQeQoFZ3rFjVajhg== | ||
|
||
[email protected]: | ||
version "2.6.9" | ||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" | ||
|
@@ -3836,7 +3841,7 @@ flat@^5.0.2: | |
resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241" | ||
integrity sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ== | ||
|
||
flatted@^3.2.9: | ||
flatted@^3.2.7, flatted@^3.2.9: | ||
version "3.3.1" | ||
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.1.tgz#21db470729a6734d4997002f439cb308987f567a" | ||
integrity sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw== | ||
|
@@ -3863,7 +3868,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" | ||
integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q== | ||
|
||
fs-extra@^8.1: | ||
fs-extra@^8.1, fs-extra@^8.1.0: | ||
version "8.1.0" | ||
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0" | ||
integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g== | ||
|
@@ -4775,6 +4780,17 @@ log-update@^4.0.0: | |
slice-ansi "^4.0.0" | ||
wrap-ansi "^6.2.0" | ||
|
||
log4js@^6.9.1: | ||
version "6.9.1" | ||
resolved "https://registry.yarnpkg.com/log4js/-/log4js-6.9.1.tgz#aba5a3ff4e7872ae34f8b4c533706753709e38b6" | ||
integrity sha512-1somDdy9sChrr9/f4UlzhdaGfDR2c/SaD2a4T7qEkG4jTS57/B3qmnjLYePwQ8cqWnUHZI0iAKxMBpCZICiZ2g== | ||
dependencies: | ||
date-format "^4.0.14" | ||
debug "^4.3.4" | ||
flatted "^3.2.7" | ||
rfdc "^1.3.0" | ||
streamroller "^3.1.5" | ||
|
||
loupe@^2.3.6: | ||
version "2.3.7" | ||
resolved "https://registry.yarnpkg.com/loupe/-/loupe-2.3.7.tgz#6e69b7d4db7d3ab436328013d37d1c8c3540c697" | ||
|
@@ -6194,6 +6210,15 @@ stdout-stderr@^0.1.9: | |
debug "^4.1.1" | ||
strip-ansi "^6.0.0" | ||
|
||
streamroller@^3.1.5: | ||
version "3.1.5" | ||
resolved "https://registry.yarnpkg.com/streamroller/-/streamroller-3.1.5.tgz#1263182329a45def1ffaef58d31b15d13d2ee7ff" | ||
integrity sha512-KFxaM7XT+irxvdqSP1LGLgNWbYN7ay5owZ3r/8t77p+EtSUAfUgtl7be3xtqtOmGUl9K9YPO2ca8133RlTjvKw== | ||
dependencies: | ||
date-format "^4.0.14" | ||
debug "^4.3.4" | ||
fs-extra "^8.1.0" | ||
|
||
string-argv@^0.3.1: | ||
version "0.3.2" | ||
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.2.tgz#2b6d0ef24b656274d957d54e0a4bbf6153dc02b6" | ||
|