Simple Logger
A lightweight, flexible logging system for ContentSmith that supports namespace-based logging with different log levels.
Features
- Namespace-based Logging: Organize logs by component and operation
- Pattern Matching: Control log levels using string prefixes or regex patterns
- Log Level Hierarchy: trace → info → warn → error
- Object Serialization: Automatically pretty-print objects
- Global Enable/Disable: Quickly toggle all logging
- Singleton Pattern: Centralized logging control
Usage
Basic Usage
import { LogManagerImpl } from '@coursebook/simple-logger';
// Get the logger instance
const logManager = LogManagerImpl.getInstance();
// Create a logger for your component
const logger = logManager.getLogger('myapp:component');
// Log at different levels
logger.trace('Detailed debugging');
logger.info('General information');
logger.warn('Warning message');
logger.error('Error occurred', { details: 'error info' });
Setting Log Levels
// Set level for specific namespace
logManager.setLogLevel('myapp:component', 'info');
// Set level for all components in 'myapp'
logManager.setLogLevel('myapp:', 'warn');
// Use regex pattern
logManager.setLogLevel(/test:\d+/, 'trace');
// Set default level for everything
logManager.setLogLevel('*', 'info');
Namespace Patterns
component:
- Matches all loggers starting with ‘component:’component:submodule
- Matches exact namespace/component:\d+/
- Matches using regex pattern*
- Matches all namespaces
Log Levels
trace
- Verbose debugging informationinfo
- General information about operation progresswarn
- Warning messages for potentially problematic situationserror
- Error messages for operation failures
When you set a log level, all levels of equal or higher severity will be logged:
- Setting level to ‘trace’ shows all logs
- Setting level to ‘info’ shows info, warn, and error
- Setting level to ‘warn’ shows only warn and error
- Setting level to ‘error’ shows only error
Enable/Disable Logging
// Disable all logging
logManager.disable();
// Re-enable logging
logManager.enable();
Object Logging
Objects are automatically pretty-printed:
logger.info('Processing config', {
server: 'localhost',
port: 3000
});
// Output:
// [INFO] [myapp:component] Processing config {
// "server": "localhost",
// "port": 3000
// }
Example Component Integration
export class FileManager {
private logger: Logger;
constructor() {
this.logger = LogManagerImpl.getInstance().getLogger('filemanager');
}
async readFile(path: string): Promise<Buffer> {
this.logger.trace('Reading file:', path);
try {
const content = await readFile(path);
this.logger.info('Successfully read file:', path);
return content;
} catch (error) {
this.logger.error('Failed to read file:', path, error);
throw error;
}
}
}
Installation
Installing from NPM (After Publishing)
Once published to NPM, the package can be installed using:
npm install @coursebook/simple-logger
This template is particularly useful for creating packages that are intended to be used locally so read the instructions below for local development.
Local Development (Without Publishing to NPM)
There are three ways to use this package locally:
Option 1: Using npm link
-
Clone this repository, install dependencies, build the package, and create a global symlink:
git clone <repository-url> cd simple-logger/packages/simple-logger # Install dependencies and build the package npm install npm run build # Create a global symlink npm link
Note: You can unlink the package later using
npm unlink
. -
In your other project where you want to use this package:
npm link @coursebook/simple-logger
-
Import the package in your project:
import { LogManagerImpl } from '@coursebook/simple-logger';
Option 2: Using local path
In your other project’s package.json
, add this package as a dependency using the local path:
{
"dependencies": {
"@coursebook/simple-logger": "file:/path/to/simple-logger"
}
}
You can use absolute or relative paths with the file:
protocol.
Then run npm install
in your project.
Now you can import the package in your project as usual.
Option 3: Using a local tarball (npm pack)
-
Follow option 1 but instead of using
npm link
, create a tarball of the package:npm pack
This will generate a file like
coursebook-simple-logger-1.0.0.tgz
. (Or whatever version you have.) You can find the tarball in the same directory as yourpackage.json
. -
In your other project, install the tarball:
npm install /absolute/path/to/simple-logger/coursebook-simple-logger-1.0.0.tgz
Or, if you copy the tarball into your project directory:
npm install ./coursebook-simple-logger-1.0.0.tgz
This method installs the package exactly as it would be published to npm, making it ideal for final testing. After this installation, you must have the package in your node_modules
directory, and you can import it as usual. You will also see the package in your package.json
file as a dependency:
{
"dependencies": {
"@coursebook/simple-logger": "file:coursebook-simple-logger-1.0.0.tgz"
}
}