Skip to content

Commit

Permalink
feat(docker): Logging
Browse files Browse the repository at this point in the history
- Switches to pino logging.
- Replaces console with pino logger.
  • Loading branch information
Mango Habanero committed Apr 4, 2023
1 parent 6d5a141 commit 09afad4
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 456 deletions.
31 changes: 14 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fastifyServerDevOptions } from '@dev/debug';
import fastifyCors from '@fastify/cors';
import formBody from '@fastify/formbody';
import fastifyPostgres from '@fastify/postgres';
Expand All @@ -8,31 +7,29 @@ import graphqlPlugin from '@plugins/graphql';
import natsPlugin from '@plugins/nats';
import redisPlugin from '@plugins/redis';
import ussdRoutes from '@routes/ussd';

import * as dotenv from 'dotenv';
import fastify, { FastifyServerOptions } from 'fastify';
import qs from 'qs';

import { config } from './config';
import { config } from '@/config';
import pino from 'pino';
import moment from 'moment-timezone';

// TODO: [Philip] - Whereas this shifts from convict to dotenv, is it ideal for externally defined variables like ones stored in vault?
dotenv.config()
export const logger = pino({
name: config.LOG.NAME,
level: config.LOG.LEVEL,
ignore: 'pid,hostname',
formatters: {
level: (label) => ({ level: label.toUpperCase() }),
},
timestamp: () => `,"time":"${moment(new Date(Date.now())).tz(config.TIMEZONE).format("DD-MM-YYYY HH:mm A")}"`,
})

let serverOptions: FastifyServerOptions = {
disableRequestLogging: config.SERVER.DISABLE_REQUEST_LOGGING,
logger: {
base: null,
level: config.LOG.LEVEL
},
logger: logger,
trustProxy: config.SERVER.TRUST_PROXY_ENABLED
}

// load dev configs if in development mode.
if (config.DEV) {
console.debug('Running in development mode.')
serverOptions = fastifyServerDevOptions
}

const app = fastify(serverOptions)

// register third-party plugins.
Expand Down Expand Up @@ -68,7 +65,7 @@ app.setErrorHandler<Error>(function (error, request, reply) {

reply.status(500).send({
error: 'INTERNAL',
message: 'Internal server error',
message: 'Internal server error.',
statusCode: 500
})
})
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const config = {
},
DEV: process.env.NODE_ENV !== 'production',
LOG: {
LEVEL: process.env.LOG_LEVEL ?? 'info'
LEVEL: process.env.LOG_LEVEL ?? 'info',
NAME: process.env.LOG_NAME ?? 'cic-ussd'
},
NATS: {
CHAIN: {
Expand Down
Loading

0 comments on commit 09afad4

Please sign in to comment.