Creating a custom serializer for complex object in a single line in the output logs

25 Views Asked by At

I have a object which is used in multiple places. I created a logger for logging in pino. Is there a way to log the following object which I get from making a restful call to a service, UserProfileService.findProfile(profileId: string): UserProfile which has the following definition or shape

   {
      userProfile:{
        name: string,
        role: string,
        hasPermission: boolean
      }
      profileDetail:{
        permissionGroups: string[],
        adminPermissionGranted: boolean,
        singleSignOn: boolean        
      }

   }


My goal is to log this object whenever I get a UserProfile response from the remote service to log it. I tried logging it but it displays in the logs as a multi-line output. I only want it to display in a single line

Here is the logger configuration:

         import { pino } from 'pino'

         const levels = {
           http: 30,
           debug: 40,
           info: 50,
           warn: 60,
           error: 70,
           fatal: 80,
        };

         const createLogger = () => {
   
         const pinoLogger = pino({
          level: 'info',
          customLevels: levels,
          transport: {
            target: 'pino-pretty',
            options: {
                colorize: true,
                colorizeObjects: true,
                translateTime: "SYS:standard",
                minimumLevel: 'info',
                timestampKey: 'time',
                levelFirst: true,
                levels: levels,
                singleLine: true
            }
          },
          formatters: {
            level: (label) => {
                return { level: label.toUpperCase() };
             }
          },
          serializers: {
            req: pino.stdSerializers.req,
            res: pino.stdSerializers.res
          }
       })
       return pinoLogger
    }
   
    export { createLogger }

Here is how I log the result

   const result = await userProfileService.findProfile('145eafgh72002as')
   logger.info({userProfile: result}, 'User Profile returned from UserProfileService')

Is there a way to write a serializer to create the output in a single line? how can I get the output in a single line?

0

There are 0 best solutions below