What is @libp2p/logger?
@libp2p/logger is a logging utility designed for use with the libp2p networking stack. It provides a simple and consistent way to log messages across different modules, making it easier to debug and monitor applications built with libp2p.
What are @libp2p/logger's main functionalities?
Creating a Logger
This feature allows you to create a logger instance for a specific module. By providing a module name, you can categorize logs, making it easier to filter and identify logs from different parts of your application.
const logger = require('@libp2p/logger')('my-module');
Logging Messages
The logger instance provides methods like `info`, `error`, `warn`, and `debug` to log messages at different levels of severity. This helps in distinguishing between informational messages, warnings, and errors.
logger.info('This is an info message'); logger.error('This is an error message');
Configuring Log Levels
You can configure the log level for your application using environment variables. This allows you to control the verbosity of logs without changing the code, which is useful for debugging in different environments.
process.env.DEBUG = 'my-module:*';
Other packages similar to @libp2p/logger
winston
Winston is a versatile logging library for Node.js that supports multiple transports (e.g., console, file, HTTP) and log levels. Compared to @libp2p/logger, Winston offers more features and flexibility, making it suitable for larger applications with complex logging requirements.
pino
Pino is a fast and low-overhead logging library for Node.js. It is designed for performance and can handle high-throughput logging. While @libp2p/logger is tailored for use with libp2p, Pino is a general-purpose logger that can be used in any Node.js application.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js. It provides structured logs, which are easy to parse and analyze. Like @libp2p/logger, Bunyan is straightforward to use, but it focuses on JSON output, which can be beneficial for log analysis tools.
@libp2p/logger
A logging component for use in js-libp2p modules
About
A logger for libp2p based on weald, a TypeScript port of the venerable debug module.
Example
import { logger } from '@libp2p/logger'
const log = logger('libp2p:my:component:name')
try {
log('something happened: %s', 'it was ok')
} catch (err) {
log.error('something bad happened: %o', err)
}
log('with this peer: %p', {})
log('and this base58btc: %b', Uint8Array.from([0, 1, 2, 3]))
log('and this base32: %t', Uint8Array.from([4, 5, 6, 7]))
$ DEBUG=libp2p:* node index.js
something happened: it was ok
something bad happened: <stack trace>
with this peer: 12D3Foo
with this base58btc: Qmfoo
with this base32: bafyfoo
Install
$ npm i @libp2p/logger
Browser <script>
tag
Loading this module through a script tag will make its exports available as Libp2pLogger
in the global namespace.
<script src="https://unpkg.com/@libp2p/logger/dist/index.min.js"></script>
API Docs
License
Licensed under either of
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.