movitext-app-logger
v1.0.22
Published
Registro de eventos de servidor
Downloads
229
Readme
NPM Package: Server Event Logger
The Server Event Logger NPM package provides an efficient solution to create, log, and track server events. With this package, you can manage event severity levels, add contextual information, and send events to a centralized monitoring system, MonitorHub.
Features
- Create and register server events with detailed context.
- Track events with varying severity levels (info, warning, error, critical).
- Seamless integration with MonitorHub for centralized event management.
- Configurable for different environments (development, staging, production).
Installation
To install the package, use the following command:
npm install movitext-app-logger
Configuration
To configure the connection to MonitorHub, import the necessary tools and set up the credentials:
import { MonitorTools } from "movitext-app-logger";
MonitorTools.setConfig({
connection: {
apiKey: "xxxxxxx.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Your API key
url: "http://monitor-address", // MonitorHub URL
},
serverEvent: {
defaultValues: {
environment: "development", // Environment setting (development, production, etc.)
},
appKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // App-specific key for event logging
},
});
Make sure to replace the placeholders (apiKey, url, and appKey) with your actual credentials.
Usage
- Create a event
Once configured, you can effortlessly log server events. Below is an example demonstrating how to create and register an event:
import { AppEvent } from "movitext-app-logger";
const event = new AppEvent({
event: "my-event-name",
description: "This is my event description",
type: "info",
severityLevel: "medium",
context: {
metadata1: 1,
metadata2: {
submetadata1: "500",
},
metadata3: [1, 2, 3],
},
});
- Updating Event Data
You can add or modify the values of the event instance fields. This is particularly useful when the event context is updated during its execution.
event.update({
track: "error-id",
description: "Descripción asignada al evento",
});
- Updating Context
You can also directly update the context field of the event. This will merge the current values with the provided ones.
event.updateContext = {
newMetada1: {
newSubmetadata1: 5,
},
};
- Changing Event Type
At any point, you can change the event type if your process has evolved. You can provide context as metadata that will be stored in the event's result field.
// * Update event to <warn>
event.warn({
warningMessage: "Warning message",
warningDetails: "Warning details",
});
// * Update event to <error>
event.error({
errorMessage: "Error message",
errorDetails: "Error details",
});
// * Update event to <critical>
event.critical({
criticalMessage: "Critical message",
criticalDetails: "Critical details",
criticalImpact: "Critical impact",
});
- Registering and Saving the Event
Once the event has all the necessary information, you can register it in MonitorHub as follows:
event.register();
- Chaining Actions
If you wish to perform multiple actions at once, you can chain each of these actions on your event:
event
.warn({
data: "warning data",
})
.error({
data: "warning data",
})
.critical({
data: "warning data",
})
.update({
track: "Process-ID",
description: "Prueba de guardado de evento",
})
.register();
Event Fields
The following options are available for constructing a context for each event:
/**
* The event name in kebab-case format.
* Example: 'user-login', 'data-update'
*/
event: string;
/**
* The type of event being logged.
* Example: 'critical', 'error', 'warn', 'info' default is 'info'
*/
type?: ServerEventType;
/**
* The severity level of the event.
* Indicates the importance or urgency of the event.
* Example: 'low', 'medium', 'high', 'critical' default is 'medium'
*/
severityLevel: ServerEventSeverityLevel;
/**
* Initial additional context or metadata related to the event.
* This can be any structure that provides more detail about the event.
*/
context: any;
/**
* A brief description of the event.
* Optional field to provide additional details about what occurred. max length is 160 characters.
*/
description?: string;
/**
* A tracking identifier that references the server event, request, ID, etc.
* Optional field used to correlate this event with others.
*/
track?: string;
/**
* An array of tags associated with the event.
* Tags can help with filtering and categorizing events.
*/
tags?: string[];
/**
* The environment where the event took place.
* Example: 'development', 'production', 'staging', 'testing'
*/
environment: Environment;
/**
* The result or outcome of the event after processing.
* Optional field that can hold any information relevant to the final state (before register).
*/
result?: any;
/**
* The timestamp when the event occurred.
* If not provided, the current date and time will be used.
*/
timestamp?: Date;
/**
* The IP address from which the event originated.
* This is the source of the event in terms of network location.
*/
sourceIp: string;
/* The `notificationChannels: NotificationChannnel[];` in the `ServerEventParams` type is defining a
property named `notificationChannels` which is an array of `NotificationChannnel` elements. This
property is used to specify the notification channels through which notifications related to the
server event should be sent.
*/
notificationChannels?: NotificationChannnel[];