0
0
Node.jsframework~30 mins

Logging with structured formats in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Logging with structured formats
📖 Scenario: You are building a simple Node.js application that logs user actions. To make logs easy to read and analyze, you will use structured JSON format for logging.
🎯 Goal: Create a Node.js script that logs user actions as JSON objects with clear keys for user, action, and timestamp.
📋 What You'll Learn
Create an object representing a user action with keys user, action, and timestamp
Add a configuration variable for the current time in ISO format
Use a function to create a structured log object combining user, action, and timestamp
Print the structured log as a JSON string
💡 Why This Matters
🌍 Real World
Structured logging helps teams quickly find and analyze logs in production systems, improving troubleshooting and monitoring.
💼 Career
Understanding structured logging is essential for DevOps roles to maintain reliable and observable applications.
Progress0 / 4 steps
1
Create the user action object
Create a constant object called userAction with these exact keys and values: user set to "alice", action set to "login", and timestamp set to an empty string "".
Node.js
Need a hint?

Use const userAction = { user: "alice", action: "login", timestamp: "" };

2
Add current timestamp configuration
Create a constant called currentTime and set it to the current date and time in ISO string format using new Date().toISOString().
Node.js
Need a hint?

Use const currentTime = new Date().toISOString(); to get the current time in ISO format.

3
Create a function to build structured log
Write a function called buildLog that takes user, action, and timestamp as parameters and returns an object with these keys and values. Then update userAction.timestamp by calling buildLog with userAction.user, userAction.action, and currentTime, and assign the returned object's timestamp to userAction.timestamp.
Node.js
Need a hint?

Define buildLog to return an object with keys user, action, and timestamp. Then update userAction.timestamp using this function.

4
Print the structured log as JSON
Use console.log to print the userAction object as a JSON string using JSON.stringify(userAction).
Node.js
Need a hint?

Use console.log(JSON.stringify(userAction)) to print the object as JSON.