└── loggers-psa-notes.txt /loggers-psa-notes.txt: -------------------------------------------------------------------------------- 1 | ########################### 2 | 🧾 What is Logging? 3 | ########################### 4 | 5 | -> Logging is the process of recording the application's activity, errors, and other useful information into a file (or console). These records are called log messages. 6 | 7 | ################################### 8 | ✅ Why is Logging Important? 9 | ################################### 10 | 11 | 1. Understand Application Behavior 12 | -> Logs help you know what the application is doing at each step. 13 | 14 | Example: “Server started”, “User logged in”, “Order placed”, etc. 15 | 16 | 2. Find Root Cause of Errors 17 | -> If your app crashes or throws an error, the logs can show what exactly happened and where. 18 | 19 | Example: NullPointerException at line 52 in a log tells you exactly where the code failed. 20 | 21 | 3. Debugging in Production 22 | -> You can’t always open a code editor in a live app. But with logs, you can see what’s going wrong without stopping the app. 23 | 24 | 4. Security & Auditing 25 | -> You can track who accessed what, which can be useful for security checks or user activity monitoring. 26 | 27 | ############################## 28 | 💬 Real Life Analogy 29 | ############################## 30 | 31 | Imagine you run a delivery business. You keep a diary of: 32 | 33 | a. Orders taken 34 | 35 | b. Orders packed 36 | 37 | c. Orders delivered 38 | 39 | d. Complaints 40 | 41 | -> Later, if a customer says, “I never got my package,” you check your diary (log) to find what happened. This is exactly how logs work in software. 42 | 43 | ################################################################ 44 | Logging Architecture one by one in a simple and practical way: 45 | ################################################################ 46 | 47 | 🔧 1) Logger 48 | 👉 What is it? 49 | -> Logger is the main object used to create log messages. (It is a class) 50 | -> It provides methods like .info(), .debug(), .error(), etc 51 | 52 | 🧩 2) Layout 53 | 👉 What is it? 54 | -> Layout decides how your log message looks. 55 | -> It defines the format/structure of each log entry. 56 | 57 | Example - 58 | [DATE] [LEVEL] - [MESSAGE] 59 | 2025-04-03 10:45:12 INFO - Server started successfully 60 | 61 | 📦 3) Appender 62 | 👉 What is it? 63 | -> Appender decides where the logs go. 64 | 65 | It could be the console, a file, a database, or even remote servers. 66 | 67 | ✅ Common Types of Appenders: 68 | a. ConsoleAppender: Logs appear on the terminal/console. 69 | b. FileAppender: Logs are saved into a file (like app.log). Most widely used approach in projects 70 | c. RollingFileAppender: Automatically creates new log files when size limit is reached. 71 | d. SMTPAppender: Sends logs via email. 72 | 73 | ####################################--Note--############################### 74 | To Analize logs file we can use tools like putty, ELK, Splunk, WinScp etc 75 | ############################################################################# 76 | 77 | ######################################## 78 | Flow of Logger how it will work: 79 | ######################################## 80 | 81 | Your Code (Logger) 82 | | 83 | v 84 | [Log Message] 85 | | 86 | v 87 | [Formatted by Layout] 88 | | 89 | v 90 | [Sent to Appender (Console/File/etc)] 91 | 92 | 93 | #################### 94 | Loggings Frameworks 95 | #################### 96 | 97 | Framework Used By Default in Spring boot? 98 | Log4j ❌ (older) 99 | Log4j2 ❌ Improved version of Log4j 100 | Logback ✅ Yes 101 | Logstash ❌ 102 | 103 | ############################################################## 104 | ✅ SLF4J (Simple Logging Facade (fuh·saad pronounce) for Java) 105 | ############################################################## 106 | 107 | -> Its an interface and is been implemented in Log4J, Log4j2, Logback, Logstash 108 | → This helps developers to **easily switch** between different logging implementations (like Log4j, Logback, etc.) without changing application code. 109 | 110 | 111 | ################################################################################# 112 | To generate log messages in file add the following in application.properties file 113 | 114 | logging.file.name=app.log 115 | 116 | ################################################################################## 117 | 118 | 119 | Example 1: 120 | ##################### 121 | 122 | import lombok.extern.slf4j.Slf4j; //Perform import from slf4J 123 | import org.springframework.stereotype.Service; 124 | 125 | @Service 126 | //@Slf4j // Enables logging without needing explicit LoggerFactory 127 | public class OrderService { 128 | 129 | private Logger log = LoggerFactory.getLogger(OrderService.class); 130 | 131 | public void processOrder(int orderId) { 132 | log.info("Processing order with ID: {}", orderId); 133 | 134 | if (orderId <= 0) { 135 | 136 | return; 137 | } 138 | 139 | log.debug("Checking inventory for order ID: {}", orderId); 140 | 141 | try { 142 | // Simulating some order processing logic 143 | Thread.sleep(2000); 144 | log.info("Order {} processed successfully!", orderId); 145 | } catch (InterruptedException e) { 146 | 147 | } 148 | } 149 | } 150 | 151 | Example 2: 152 | ########################### 153 | 154 | import lombok.extern.slf4j.Slf4j; //Perform import from slf4J 155 | import org.springframework.stereotype.Service; 156 | 157 | @Service 158 | //@Slf4j // Enables logging without needing explicit LoggerFactory 159 | public class OrderService { 160 | 161 | private Logger log = LoggerFactory.getLogger(OrderService.class); 162 | 163 | public void processOrder(int orderId) { 164 | log.info("Processing order with ID: {}", orderId); 165 | 166 | if (orderId <= 0) { 167 | log.error("Invalid order ID: {}", orderId); 168 | return; 169 | } 170 | 171 | log.debug("Checking inventory for order ID: {}", orderId); 172 | 173 | try { 174 | // Simulating some order processing logic 175 | Thread.sleep(2000); 176 | log.info("Order {} processed successfully!", orderId); 177 | } catch (InterruptedException e) { 178 | log.error("Error processing order: {}", orderId, e); 179 | } 180 | } 181 | } 182 | 183 | ################################### 184 | Log Levels & Their Purpose 185 | ################################### 186 | 187 | Each log level serves a different purpose: 188 | 189 | Log Level Purpose 190 | TRACE Most detailed logs, used for debugging deep-level issues. Rarely enabled in production. 191 | DEBUG Debugging information useful for developers to track program flow. 192 | INFO General application information (e.g., "Application Started Successfully"). Default in Spring Boot. 193 | WARN Indications of potential issues (e.g., "Disk Space Low"). 194 | ERROR Severe issues that may cause the application to fail. 195 | 196 | 197 | ############################################ 198 | Example on how to implement logs in projects 199 | ########################################### 200 | 201 | 202 | ##################### 203 | Service Layer 204 | ##################### 205 | 206 | package com.example.demo.service; 207 | 208 | import lombok.extern.slf4j.Slf4j; 209 | import org.springframework.stereotype.Service; 210 | 211 | @Service 212 | @Slf4j 213 | public class UserService { 214 | 215 | public void registerUser(String username) { 216 | log.trace("Entering registerUser method with username: {}", username); 217 | 218 | if (username == null || username.isEmpty()) { 219 | log.warn("Username is empty or null!"); 220 | return; 221 | } 222 | 223 | log.debug("Checking if username {} is already taken...", username); 224 | 225 | if ("admin".equalsIgnoreCase(username)) { 226 | log.error("Registration failed: Username '{}' is reserved!", username); 227 | return; 228 | } 229 | 230 | log.info("User '{}' registered successfully!", username); 231 | log.trace("Exiting registerUser method..."); 232 | } 233 | 234 | public void getUserDetails(String username) { 235 | log.trace("Entering getUserDetails method for username: {}", username); 236 | 237 | if ("testUser".equalsIgnoreCase(username)) { 238 | log.debug("Fetching details for test user."); 239 | log.info("User details found for '{}'", username); 240 | } else { 241 | log.warn("User '{}' not found in the system!", username); 242 | } 243 | 244 | log.trace("Exiting getUserDetails method..."); 245 | } 246 | } 247 | 248 | ############################# 249 | application.properties file 250 | ############################# 251 | 252 | logging.level.root=DEBUG 253 | logging.level.com.example.demo.service=TRACE 254 | logging.file.name=app.log 255 | 256 | 257 | ################### 258 | Modify Main Class 259 | ################### 260 | 261 | package com.example.demo; 262 | 263 | import com.example.demo.service.UserService; 264 | import org.springframework.boot.CommandLineRunner; 265 | import org.springframework.boot.SpringApplication; 266 | import org.springframework.boot.autoconfigure.SpringBootApplication; 267 | import org.springframework.context.ApplicationContext; 268 | import org.springframework.context.annotation.Bean; 269 | 270 | @SpringBootApplication 271 | public class MainApplication { 272 | 273 | public static void main(String[] args) { 274 | SpringApplication.run(MainApplication.class, args); 275 | } 276 | 277 | @Bean 278 | CommandLineRunner run(ApplicationContext ctx) { 279 | return args -> { 280 | UserService userService = ctx.getBean(UserService.class); 281 | 282 | userService.registerUser("john_doe"); 283 | userService.registerUser("admin"); 284 | userService.registerUser(""); 285 | 286 | userService.getUserDetails("testUser"); 287 | userService.getUserDetails("unknownUser"); 288 | }; 289 | } 290 | } 291 | 292 | --------------------------------------------------------------------------------------------- 293 | 294 | Expected Output for debug 295 | 296 | TRACE - Entering registerUser method with username: john_doe 297 | DEBUG - Checking if username john_doe is already taken... 298 | INFO - User 'john_doe' registered successfully! 299 | TRACE - Exiting registerUser method... 300 | 301 | TRACE - Entering registerUser method with username: admin 302 | DEBUG - Checking if username admin is already taken... 303 | ERROR - Registration failed: Username 'admin' is reserved! 304 | 305 | TRACE - Entering registerUser method with username: 306 | WARN - Username is empty or null! 307 | 308 | TRACE - Entering getUserDetails method for username: testUser 309 | DEBUG - Fetching details for test user. 310 | INFO - User details found for 'testUser' 311 | TRACE - Exiting getUserDetails method... 312 | 313 | TRACE - Entering getUserDetails method for username: unknownUser 314 | WARN - User 'unknownUser' not found in the system! 315 | TRACE - Exiting getUserDetails method... 316 | 317 | ------------------------------------------------------------------------------------------------------ 318 | 319 | Explanation of Log Levels Used 320 | 321 | ✅ TRACE → Used to track method entry and exit points. Helps when debugging method flow. 322 | ✅ DEBUG → Provides additional context, such as checking if a username exists. 323 | ✅ INFO → General information, like confirming a successful user registration. 324 | ✅ WARN → Used when input is missing (empty username) or when the user is not found. 325 | ✅ ERROR → Used for serious issues, like trying to register with a reserved username. 326 | 327 | How to Control Log Levels in application.properties 328 | 329 | logging.level.root=DEBUG 330 | logging.file.name=app.log 331 | 332 | 🔹 Setting TRACE level for this package means all logs (TRACE, DEBUG, INFO, WARN, ERROR) will be printed. 333 | 🔹 If we change it to INFO, only INFO, WARN, and ERROR logs will be recorded. 334 | 335 | ############################################ 336 | ELK (Elasticsearch, Logstash, Kibana) 337 | ############################################ 338 | 339 | ELK (Elasticsearch, Logstash, Kibana) is a powerful stack used for log analysis, monitoring, and visualization in real-time. It helps in debugging, performance monitoring, and security auditing for applications, including Spring Boot apps. 340 | 341 | a. Logstash - A data processing pipeline that collects, processes, and forwards logs to Elasticsearch. 342 | 343 | b. Elasticsearch - A search and analytics engine that stores logs and allows querying. Provides fast retrieval of logs with filtering capabilities. 344 | 345 | c. Kibana - A visualization tool for logs stored in Elasticsearch. Helps create dashboards, graphs, and alerts for log monitoring. 346 | 347 | ############### 348 | ELK Setup - 349 | ############### 350 | 351 | ------------------------------------------ 352 | Setps to setup elastic search on windows 353 | ----------------------------------------- 354 | 355 | Step 1: https://www.elastic.co/downloads/elasticsearch 356 | 357 | Step 2: Start the service 358 | 359 | ------------------------ 360 | Steps to download kibana 361 | ------------------------ 362 | 363 | Step 1: https://www.elastic.co/downloads/kibana 364 | Step 2: Start the service 365 | 366 | --------------------------- 367 | Steps to download logstash 368 | --------------------------- 369 | 370 | Step 1: https://www.elastic.co/downloads/logstash 371 | Step 2: Start the service - Run bin/logstash -f logstash.conf 372 | 373 | ---------------------------------------------------------------------------------------------------------- 374 | 375 | $$$$$$$$$$$$$$$$$$$$$$$$$$$-----Further notes will be updated soon--------$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 376 | 377 | --------------------------------------------------------------------------------