lego-logger
v1.0.31
Published
Logging solution for applications
Readme
Lego Logger
A lightweight and flexible logging library, part of a solution developed for Corebiz clients. lego-logger allows you to register events, errors, and other important information in your VTEX applications, integrating with a backend API for centralized log storage.
✨ Features
- Supports multiple log levels:
info,success,warn,error,debug - Dynamically generates the API URL based on the VTEX account and workspace
- Easy integration with VTEX apps and environments
- Securely sends logs using a
clientKeyfor authentication - Flexible and extensible for various use cases
📦 Installation
Install the library via npm or yarn:
npm install lego-logger
# or
yarn add lego-logger🚀 Usage
1. Import the library
import { Logger } from 'lego-logger'2. Configure the Logger
Instantiate the logger with the following parameters:
clientKey: A unique key provided for each clientaccount: The VTEX account nameworkspace: The VTEX workspace name
The API URL is dynamically generated based on these parameters.
const CLIENT_KEY = 'your-client-key'
const ACCOUNT = 'your-account'
const WORKSPACE = 'your-workspace'
const logger = new Logger(CLIENT_KEY, ACCOUNT, WORKSPACE)3. Log events
Use the logger methods to register events at different levels:
// Success log
await logger.success({
message: 'User successfully logged in',
identifyid: 'example'
payload: { data: 'example' },
response: { data: 'example' }
additionaldata: { data: 'example' }
})
// Error log
await logger.error({
message: 'Failed to fetch user data',
identifyid: 'example'
payload: { data: 'example' },
response: { data: 'example' }
additionaldata: { data: 'example' }
})📚 API Reference
Constructor
new Logger(clientKey: string, account: string, workspace: string)Parameters:
clientKey(string): Unique client identification keyaccount(string): VTEX account nameworkspace(string): VTEX workspace name
Methods
1. success(params: LogParams)
Logs a success event.
2. info(params: LogParams)
Logs an informational event.
3. warn(params: LogParams)
Logs a warning.
4. error(params: LogParams)
Logs an error.
5. debug(params: LogParams)
Logs a debug event.
🔧 LogParams
Object with the following fields:
| Field | Type | Description |
|------------------|----------|--------------------------------------------------|
| message | string | Main log message (required) |
| identifyid | string | Optional identifier |
| payload | any | Object Payload data |
| response | any | Object Response data |
| additionaldata | any | Object with extra information |
🌐 Dynamic API URL
The API URL is dynamically generated based on the given account and workspace:
const logger = new Logger('client-key', 'account', 'workspace')
// Generates the URL:
https://workspace--account.myvtex.com/_v/logs/register❗ Important Notes
Make sure the endpoint is accessible — this requires the Admin App Lego-Logger to be properly installed in your VTEX account.
Ensure the provided clientKey is valid and active.
If the log fails to send due to network issues or server errors, a message will appear in the console:
Error sending log: Failed to send log to API.📬 Support
For questions or support, contact: email
