69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import { LogLevel, createLogger, getDefaultLogger } from "./logger";
|
|
|
|
// Example 1: Using default logger (logs to ./logs folder)
|
|
const defaultLog = getDefaultLogger();
|
|
|
|
console.log("=== Default Logger Example ===");
|
|
defaultLog.debug("a debug message");
|
|
defaultLog.info("a info message");
|
|
defaultLog.notice("a notice message");
|
|
defaultLog.warning("a warning message");
|
|
defaultLog.error("a error message");
|
|
defaultLog.critical("a critical message");
|
|
defaultLog.alert("a alert message");
|
|
defaultLog.emergency("a emergency message");
|
|
|
|
// Example 2: Custom logger with specific folder
|
|
const customLog = createLogger({
|
|
logDir: "./custom-logs",
|
|
minLevel: LogLevel.WARNING, // Only log WARNING and above
|
|
filePrefix: "payment-api",
|
|
});
|
|
|
|
console.log("\n=== Custom Logger Example ===");
|
|
customLog.debug("this will not be logged (below min level)");
|
|
customLog.info("this will not be logged (below min level)");
|
|
customLog.warning("a warning message that will be logged");
|
|
customLog.error("a error message that will be logged");
|
|
customLog.critical("a critical message that will be logged");
|
|
|
|
// Example 3: Logger with additional arguments
|
|
const apiLog = createLogger({
|
|
logDir: "./api-logs",
|
|
filePrefix: "mandiri-api",
|
|
maxFileSizeMB: 5,
|
|
});
|
|
|
|
console.log("\n=== API Logger Example with Objects ===");
|
|
apiLog.info("Payment received", {
|
|
amount: 100000,
|
|
virtualAccount: "1234567890",
|
|
timestamp: new Date(),
|
|
});
|
|
|
|
apiLog.error("Payment failed", {
|
|
error: "Insufficient balance",
|
|
virtualAccount: "1234567890",
|
|
attemptedAmount: 500000,
|
|
});
|
|
|
|
// Example 4: Different log levels with context
|
|
const bankLog = createLogger({
|
|
logDir: "./bank-integration",
|
|
filePrefix: "bank-sync",
|
|
});
|
|
|
|
console.log("\n=== Bank Integration Logger Example ===");
|
|
bankLog.debug("Starting database sync");
|
|
bankLog.info("Connected to bank API");
|
|
bankLog.notice("Processing 1000 transactions");
|
|
bankLog.warning("API rate limit approaching");
|
|
bankLog.error("Connection timeout, retrying...");
|
|
bankLog.critical("Multiple failures detected");
|
|
bankLog.alert("System performance degraded");
|
|
bankLog.emergency("All bank connections failed");
|
|
|
|
console.log(
|
|
"\nLogger examples completed. Check the log files in respective directories."
|
|
);
|