Logger
Global singleton log manager that centrally manages log output for all plugins.
import { Logger } from '@meng-xi/vite-plugin/logger'Class Definition
class Logger {
static create(options: LoggerOptions): Logger
static unregister(pluginName: string): void
static destroy(): void
createPluginLogger(pluginName: string): PluginLogger
}Static Methods
create
Create a logger (factory method), register plugin config and return the singleton instance.
static create(options: LoggerOptions): Logger| Parameter | Type | Description |
|---|---|---|
| options | LoggerOptions | Logger config |
Returns
Logger - Singleton instance
TIP
Multiple create calls return the same instance, but each call registers the corresponding plugin's log config.
Example
const logger = Logger.create({ name: 'my-plugin', enabled: true })unregister
Unregister a plugin's log config.
static unregister(pluginName: string): void| Parameter | Type | Description |
|---|---|---|
| pluginName | string | Plugin name to unregister |
Typically called automatically by BasePlugin.destroy().
Example
Logger.unregister('my-plugin')destroy
Destroy the singleton instance and release all resources.
static destroy(): voidClears all registered plugin configs and resets the singleton. Mainly used in test scenarios.
Example
afterEach(() => {
Logger.destroy()
})Instance Methods
createPluginLogger
Create a plugin log proxy object.
createPluginLogger(pluginName: string): PluginLogger| Parameter | Type | Description |
|---|---|---|
| pluginName | string | Plugin name |
Returns
PluginLogger - Plugin log proxy object
WARNING
This method is for internal BasePlugin use. You generally don't need to call it directly.
Example
const logger = Logger.create({ name: 'my-plugin', enabled: true })
const pluginLogger = logger.createPluginLogger('my-plugin')
pluginLogger.info('Info message')
pluginLogger.success('Success message')Singleton Pattern
Logger uses the singleton pattern to ensure only one log manager instance globally.
const logger1 = Logger.create({ name: 'plugin-a', enabled: true })
const logger2 = Logger.create({ name: 'plugin-b', enabled: false })
// logger1 and logger2 are the same Logger instance
// But each plugin's log config is managed independentlyPlugin-Level Control
Each plugin's logging can be controlled independently.
Logger.create({ name: 'plugin-a', enabled: true })
Logger.create({ name: 'plugin-b', enabled: false })
const logger = Logger.create({ name: 'plugin-a', enabled: true })
const loggerA = logger.createPluginLogger('plugin-a')
const loggerB = logger.createPluginLogger('plugin-b')
loggerA.info('This will be output')
loggerB.info('This will not be output')