Gathering detailed insights and metrics for logstash-winston-3
Gathering detailed insights and metrics for logstash-winston-3
Gathering detailed insights and metrics for logstash-winston-3
Gathering detailed insights and metrics for logstash-winston-3
winston3-logstash-transport
A winston@3 replacement for both winston-logstash and winston-logstash-udp to facilitate either TCP or UDP traffic to logstash
@chitkosarvesh/winston-logstash
Adds Logstash support to Winston 3
winston-logstash-salim
A winston@3 replacement for both winston-logstash and winston-logstash-udp to facilitate either TCP or UDP traffic to logstash
@caal-15/winston3-logstash-transport
A winston@3 replacement for both winston-logstash and winston-logstash-udp to facilitate either TCP or UDP traffic to logstash
This NPM module provides a LogService class for centralized logging in microservices. It uses Winston library to format and send logs to Logstash for further processing by Elasticsearch. This class can be used in your microservices to centralize and standardize log management.
npm install logstash-winston-3
Typescript
Module System
Node Version
NPM Version
67.8
Supply Chain
96.9
Quality
73.3
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
2,080
Last Day
1
Last Week
9
Last Month
29
Last Year
1,072
MIT License
1 Stars
10 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Feb 10, 2025
Minified
Minified + Gzipped
Latest Version
2.1.3
Package Id
logstash-winston-3@2.1.3
Unpacked Size
27.57 kB
Size
7.94 kB
File Count
7
NPM Version
9.5.0
Node Version
18.15.0
Published on
Apr 23, 2023
Cumulative downloads
Total Downloads
3
2
LogService is an npm package that simplifies log management for your application using the winston module. It supports sending logs to a logstash server and also provides methods for logging errors, warnings, debugging, and info messages.
1npm install --save logstash-winston-3
To use LogService, you need to create an instance of the LogService class by providing it with the required parameters.
1import { LogService } from "logstash-winston-3"; 2 3const logger = LogService.getInstance({ 4 serviceName: "my-app", 5 logstashHost: "localhost", 6 logstashPort: 5000, 7});
Once you have created an instance of LogService, you can use its methods to log messages. The available log levels are "error", "warn", "info", and "debug". You can also add additional data to your logs using the optional "meta" argument.
1logger.info("Hello World"); 2logger.error("An error occurred", { errorCode: 500 });
You can customize the log configuration using the following methods:
1logger.setLevel("debug"); // Changes the current log level 2logger.setSilent(true); // Disables all logging 3logger.setPrettyPrint(true); // Makes logs more readable 4logger.setJsonStringify(true); // Enables JSON serialization of logs
To log unhandled errors in your application, you can use the "logUnhandledErrors" method.
1logger.logUnhandledErrors();
You can use the "logStackTrace" method to log the stack trace of an error.
1try { 2 // code that might potentially throw an error 3} catch (error) { 4 logger.logStackTrace(error); 5}
To log nested objects, you can use the "logNestedObject" method. The depth of the object can also be configured using the optional "maxDepth" argument.
1const myObject = { 2 name: "John", 3 age: 30, 4 address: { 5 street: "123 Main St", 6 city: "New York", 7 }, 8}; 9 10logger.logNestedObject(myObject, 2); // logs the myObject up to 2 levels deep
You can use the "logWithContext" method to add context to your logs. The context is added as a property to the logged object.
1logger.logWithContext("database", "info", "Connected to database");
To use LogService in your NestJS application, you can create a logger provider that will create an instance of LogService and inject it into your services.
1import { Provider } from "@nestjs/common"; 2import { LogService } from "logstash-winston-3"; 3 4export const LoggerProvider: Provider = { 5 provide: "Logger", 6 useFactory: () => { 7 return LogService.getInstance({ 8 serviceName: "my-app", 9 logstashHost: "localhost", 10 logstashPort: 5000, 11 }); 12 }, 13};
In this example, we are creating a provider named "Logger" that will create an instance of LogService using the parameters specified.
Now that we have created a logger provider, we can inject it into our services using the "@Inject()" decorator.
1import { Injectable, Inject } from "@nestjs/common"; 2 3@Injectable() 4export class MyService { 5 constructor(@Inject("Logger") private readonly logger: LogService) {} 6 7 async myMethod() { 8 try { 9 // some code that might throw an error 10 } catch (error) { 11 this.logger.error("An error occurred", { error }); 12 throw error; 13 } 14 } 15}
In this example, we are injecting the "Logger" provider into our service and using it to log errors.
You can also use the logger with context to add additional information to your logs.
1async myMethod() { 2 this.logger.logWithContext('myMethod', 'info', 'Starting execution'); 3 try { 4 // some code 5 this.logger.logWithContext('myMethod', 'info', 'Execution successful'); 6 } catch (error) { 7 this.logger.logWithContext('myMethod', 'error', 'An error occurred', { error }); 8 throw error; 9 } 10}
In this example, we are adding the name of the method ("myMethod") as the context for our logs. This makes it easier to trace which method generated the log message.
If you want to keep the default winston logs in the console, you can use the following code:
1import { Provider } from "@nestjs/common"; 2import { LogService } from "logstash-winston-3"; 3 4export const LoggerProvider: Provider = { 5 provide: "Logger", 6 useFactory: () => { 7 return LogService.getInstance({ 8 serviceName: "my-app", 9 logstashHost: "localhost", 10 logstashPort: 5000, 11 callback: (level: "error" | "warn" | "debug", message: string) => { 12 logger[level](`${message}`); 13 }, 14 }); 15 }, 16};
How can I configure LogService to send logs to Logstash?
LogService can be configured to send logs to a Logstash server by specifying the Logstash host and port when creating an instance of LogService. You can use the getInstance
method to create an instance of LogService with the following parameters:
1const logger = LogService.getInstance({ 2 serviceName: "my-app", 3 logstashHost: "localhost", 4 logstashPort: 5000, 5});
In this example, "my-app" is the name of the service for which logs are being collected. You should replace "localhost" with the IP address or hostname of your Logstash server, and "5000" with the port on which Logstash is listening for logs.
How can I add metadata to logs?
You can add metadata to logs by passing a JSON object as the second argument to the appropriate logging method. For example:
1logger.info("Info", { metadataKey: "metadataValue" });
In this example, we have added a key-value pair to the log's metadata as a JSON object.
How can I customize the format of logs?
LogService uses Winston for log management, so you can customize the format of logs using Winston's formatting options. You can use the following options:
1logger.setPrettyPrint(true); // For more readable formatting 2logger.setJsonStringify(true); // For serializing logs to JSON
How can I configure the minimum log level?
You can configure the minimum log level using LogService's setLevel
method. The available log levels are "error", "warn", "info", and "debug".
1logger.setLevel("debug");
In this example, we have configured the minimum log level to "debug".
How can I use LogService with a framework other than NestJS?
LogService can be used with other popular frameworks or libraries. You can create an instance of LogService with the appropriate parameters and use it to log events in your application.
How can I use LogService with different log transport protocols?
LogService can be used with different log transport protocols such as syslog or fluentd using the corresponding log transport libraries. The log transport libraries can be installed via npm and imported into your application.
How can I troubleshoot common errors when using LogService?
If you encounter errors when using LogService, you can check that the configuration parameters are correct and that the required libraries are installed. You can also check the server logs to detect errors and warnings that may be related to the use of LogService. If you are not sure of the cause of the error, you can consult the documentation or reach out to the LogService community for help.
Contributions to LogService are always welcome! If you find a bug or want to suggest a new feature, please open an issue on the GitHub repository.
If you want to contribute code, please fork the repository and create a new branch for your feature or bug fix. Once you have made your changes, create a pull request and we will review your code.
Please make sure to follow our code of conduct and our contribution guidelines when contributing to LogService. We ask that you be respectful and professional in all interactions with other contributors and maintainers.
LogService is distributed under the MIT license. See the LICENSE file for details.
This package was created by Cerfio. Please consider starring the project on Github if you find it helpful! ✨
No vulnerabilities found.
No security vulnerabilities found.
Last Day
0%
1
Compared to previous day
Last Week
0%
9
Compared to previous week
Last Month
16%
29
Compared to previous month
Last Year
162.1%
1,072
Compared to previous year