Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Abstract logger TypeScript interface with a dummy logger that does nothing, useful for libraries.
The ts-log npm package provides a simple logging interface for TypeScript applications. It allows developers to create custom loggers and use them throughout their codebase. The package defines a minimal set of logging methods that can be implemented to create a logger compatible with the ts-log interface.
Creating a custom logger
This feature allows you to create a custom logger and use various methods like debug, info, warn, and error to log messages at different levels.
{"createLogger": "const { Logger } = require('ts-log');\nconst myLogger = new Logger();\nmyLogger.debug('This is a debug message.');\nmyLogger.info('This is an info message.');\nmyLogger.warn('This is a warning message.');\nmyLogger.error('This is an error message.');"}
Extending the logger
This feature demonstrates how to extend the Logger class to add custom logging methods such as a trace method.
{"extendLogger": "const { Logger } = require('ts-log');\nclass MyLogger extends Logger {\n trace(message) {\n // Custom trace method implementation\n console.log('TRACE: ' + message);\n }\n}\nconst myLogger = new MyLogger();\nmyLogger.trace('This is a trace message.');"}
Winston is a multi-transport async logging library for Node.js. It is designed to be a simple and universal logging library with support for multiple transports. Compared to ts-log, winston offers more built-in features, including transports for logging to various outputs, custom log levels, and formatting options.
Pino is a very low overhead Node.js logger, which is inspired by Bunyan. It provides a different set of trade-offs compared to ts-log, focusing on performance and including features like child loggers, custom serializers, and support for the browser.
Log4js is a logging framework for Node.js, which includes features like multiple appenders, layouts, and categories. It is more complex and configurable than ts-log, offering a more comprehensive logging solution for larger applications.
Abstract logger TypeScript interface along with a dummy logger that does nothing.
Useful for libraries wanting to provide a pluggable logger that does nothing by default.
This package is distributed via npm
npm install ts-log
import { dummyLogger, ILogger } from "ts-log";
import * as fs from "fs";
// example custom logger that logs to a file
class FileLogger implements ILogger {
private readonly fd: number;
public constructor(filename: string) {
this.fd = fs.openSync(filename, "a");
}
public trace(message?: any, ...optionalParams: any[]): void {
this.append("TRACE", `${message} ${JSON.stringify(optionalParams)}`);
}
public debug(message?: any, ...optionalParams: any[]): void {
this.append("DEBUG", `${message} ${JSON.stringify(optionalParams)}`);
}
public info(message?: any, ...optionalParams: any[]): void {
this.append("INFO ", `${message} ${JSON.stringify(optionalParams)}`);
}
public warn(message?: any, ...optionalParams: any[]): void {
this.append("WARN ", `${message} ${JSON.stringify(optionalParams)}`);
}
public error(message?: any, ...optionalParams: any[]): void {
this.append("ERROR", `${message} ${JSON.stringify(optionalParams)}`);
}
private append(type: string, message: string) {
fs.writeSync(this.fd, `${new Date().toISOString()} ${type} ${message}\n`);
}
}
// example class that uses the logger
class Calculator {
// accept the logger in the constructor, defaulting to dummy logger
public constructor(private readonly log: ILogger = dummyLogger) {}
public sum(a: number, b: number) {
const result = a + b;
// call the logger
this.log.info(`summing ${a} + ${b} = ${result}`, a, b, result);
return result;
}
}
// don't define the logger, defaults to dummy logger that does not do anything
const calculator1 = new Calculator();
// use the build-in console as the logger
const calculator2 = new Calculator(console);
// use the custom file logger
const calculator3 = new Calculator(new FileLogger("log.txt"));
// run the calculator
calculator1.sum(2, 3);
calculator2.sum(-4, 1);
calculator3.sum(6, 3);
yarn start
to start the example application.yarn build
to build the production version.yarn test
to run tests.yarn coverage
to gather code coverage.yarn lint
to lint the codebase.yarn prettier
to run prettier.yarn audit
to run all pre-commit checks (prettier, build, lint, test)FAQs
Abstract logger TypeScript interface with a dummy logger that does nothing, useful for libraries.
The npm package ts-log receives a total of 2,055,580 weekly downloads. As such, ts-log popularity was classified as popular.
We found that ts-log demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.