@mr_fozan/log.js
v1.0.5
Published
A logging library using Mongoose for MongoDB
Downloads
95
Maintainers
Readme
@mr_fozan/log.js
A logging library for Node.js using Mongoose and MongoDB. This library allows you to easily create and retrieve log entries in your MongoDB database.
Features
- Flexible Logging: Create log entries with custom fields.
- Date Handling: Automatically records the timestamp of log entries.
- Model-based: Utilizes Mongoose for schema definition and data management.
Installation
To install the library, use npm or yarn:
npm install @mr_fozan/log.js
or
yarn add @mr_fozan/log.js
Usage
Setting Up
First, require the library and configure it with your MongoDB connection string and model name.
const Log = require('@mr_fozan/log.js');
// Initialize the Log class with your database configuration
const log = new Log({
databaseKey: 'your-mongodb-connection-string',
name: 'Log' // Model name
});
Creating a Log Entry
Use the create
method to add new log entries.
const params = {
ids: [1, 2, 3], // Example IDs
text: 'This is a log entry'
};
log.create(params)
.then(createdLog => {
console.log('Log entry created:', createdLog);
})
.catch(error => {
console.error('Error creating log entry:', error);
});
Retrieving Log Entries
Use the get
method to find log entries by ID.
const idToSearch = 1;
log.get(idToSearch)
.then(logs => {
console.log('Found logs:', logs);
})
.catch(error => {
console.error('Error retrieving logs:', error);
});
API
Log
Constructor
new Log({ databaseKey, name })
databaseKey
(string): The MongoDB connection string.name
(string): The name of the Mongoose model.
create(params)
- Parameters
params
(Object): Parameters for the log entry.ids
(Array): An array of IDs associated with the log entry.text
(string): The log message.date
(number, optional): The timestamp of the log entry.
- Returns: A promise that resolves to the created log entry.
- Parameters
get(id)
- Parameters
id
(string): The ID to search for in the logs.
- Returns: A promise that resolves to an array of log entries that match the ID.
- Parameters
License
This project is licensed under the ISC License - see the LICENSE file for details.
Contact
For any questions or support, please open an issue on the GitHub repository.
You can copy and paste this content into a file named README.md
in your project directory. Make sure to update the placeholder values, such as 'your-mongodb-connection-string'
, with actual information relevant to your project.