A helper utility for logging of WebdriverIO packages with enhanced features like progress logging and masking patterns for secure logging
—
Global configuration system for managing log levels, masking patterns, and file output with support for per-logger customization and environment variable integration.
Configure log levels for multiple loggers at once and set a global default log level.
/**
* Configure log levels for multiple loggers and set global default
* @param logLevels - Object mapping logger names to log levels (optional)
* @param wdioLogLevel - Global default log level (optional, defaults to 'info')
*/
logger.setLogLevelsConfig(
logLevels?: Record<string, LogLevelDesc>,
wdioLogLevel?: LogLevelDesc
): void;Usage Examples:
import logger from '@wdio/logger';
// Set global default and specific logger levels
logger.setLogLevelsConfig({
'webdriver': 'debug', // Show all webdriver logs
'selenium': 'warn', // Only warnings and errors for selenium
'browser': 'info', // Standard info level for browser
'devtools': 'silent' // Suppress devtools logs
}, 'info'); // Global default level
// Create loggers - they'll use the configured levels
const wdLog = logger('webdriver'); // Uses debug level
const seleniumLog = logger('selenium'); // Uses warn level
const appLog = logger('myApp'); // Uses global default (info)
// Works with logger name prefixes (sub-loggers inherit parent config)
logger.setLogLevelsConfig({
'api': 'debug', // All api:* loggers will use debug
'api:cache': 'warn' // But api:cache specifically uses warn
});
const apiLog = logger('api:request'); // Uses debug (inherits from 'api')
const cacheLog = logger('api:cache'); // Uses warn (specific config)Configure masking patterns globally or per-logger to obfuscate sensitive information in log output.
/**
* Set masking patterns for secure logging of sensitive data
* @param pattern - Either a global pattern string or object mapping logger names to patterns
*/
logger.setMaskingPatterns(
pattern: string | Record<string, string>
): void;Pattern Formats:
--key=[^ ]*/--key=([^ ]*)/i--key=[^ ]*,--secret=[^ ]*/--key=([^ ]*)/i (only groups are masked)Usage Examples:
import logger from '@wdio/logger';
// Global masking patterns for all loggers
logger.setMaskingPatterns('/--key=([^ ]*)/i,/--secret=([^ ]*)/i,/token=([^ ]*)/');
const log = logger('auth');
log.info('Command: wdio --key=secretKey123 --token=abc123');
// Output: "Command: wdio --key=**MASKED** --token=**MASKED**"
// Per-logger masking patterns
logger.setMaskingPatterns({
'database': '/password=([^ ]*)/i,/connectionString=([^ ]*)/i',
'api': '/authorization:\\s*bearer\\s+([^ ]*)/i',
'cache': '/redis_url=([^ ]*)/i'
});
const dbLog = logger('database');
const apiLog = logger('api');
dbLog.info('Connecting with password=mypass123');
// Output: "Connecting with password=**MASKED**"
apiLog.info('Headers: { authorization: Bearer token123 }');
// Output: "Headers: { authorization: Bearer **MASKED** }"Additional configuration utilities for managing logger lifecycle and state.
/**
* Wait for log file buffer to be flushed
* Prevents log loss when process exits quickly
* @returns Promise that resolves when buffer is flushed
*/
logger.waitForBuffer(): Promise<void>;
/**
* Clear and close the log file stream
* Useful for cleanup or log rotation
*/
logger.clearLogger(): void;Usage Examples:
import logger from '@wdio/logger';
// Ensure all logs are written before process exit
process.on('exit', async () => {
await logger.waitForBuffer();
});
// Rotate or clear log files
async function rotateLogs() {
await logger.waitForBuffer(); // Ensure current logs are written
logger.clearLogger(); // Close current log file
// Log file will be recreated on next log message if WDIO_LOG_PATH is set
const log = logger('app');
log.info('New log session started');
}
// Graceful shutdown with log flushing
async function shutdown() {
const log = logger('shutdown');
log.info('Application shutting down...');
await logger.waitForBuffer(); // Wait for logs to be written
process.exit(0);
}The configuration system integrates with environment variables for deployment flexibility:
// Environment variables that affect configuration:
interface EnvironmentConfig {
/** Default log level when not explicitly configured */
WDIO_LOG_LEVEL?: LogLevelDesc;
/** When set, changes default level to 'trace' for debugging */
WDIO_DEBUG?: string;
/** Path for log file output (creates file if specified) */
WDIO_LOG_PATH?: string;
/** Global masking patterns (comma-separated regex strings) */
WDIO_LOG_MASKING_PATTERNS?: string;
}Usage Examples:
# Set via environment variables
export WDIO_LOG_LEVEL=debug
export WDIO_LOG_PATH=/tmp/wdio.log
export WDIO_LOG_MASKING_PATTERNS='/--key=([^ ]*)/i,/password=([^ ]*)/i'
# Or inline with command
WDIO_DEBUG=1 WDIO_LOG_PATH=./test.log npm run test// Environment variables take precedence unless overridden
process.env.WDIO_LOG_LEVEL = 'debug'; // Sets global default
logger.setLogLevelsConfig({
'specific': 'warn' // Overrides global default for 'specific' logger
});
// 'specific' logger uses 'warn', others use 'debug' from env varConfiguration values are applied in this priority order (highest to lowest):
setLevel(), setLogLevelsConfig())WDIO_LOG_LEVEL, WDIO_DEBUG)Examples:
// Priority demonstration
process.env.WDIO_LOG_LEVEL = 'debug'; // Environment setting
logger.setLogLevelsConfig({}, 'warn'); // Method call overrides env
// Global level is now 'warn', not 'debug'
logger.setLogLevelsConfig({
'api': 'error' // Specific override
});
// 'api' logger uses 'error', others use global 'warn'
logger.setLevel('api', 'trace'); // Direct override
// 'api' logger now uses 'trace' (highest priority)Install with Tessl CLI
npx tessl i tessl/npm-wdio--logger