sniplog
v1.0.0
Published
SnipLog SDK for error tracking - supports both Node.js/Express backend and browser frontend
Downloads
185
Maintainers
Readme
SnipLog SDK
Enterprise-grade error tracking SDK for Node.js/Express backends and browser frontends.
Installation
npm install sniplog-sdkUsage
Node.js / Express Backend (Current Focus)
Basic Setup
const express = require('express');
const SnipLog = require('sniplog-sdk');
const app = express();
// Initialize SnipLog
const sniplog = new SnipLog({
endpoint: 'http://localhost:3000/api/errors',
projectKey: 'your-project-key',
autoCaptureExceptions: true, // Auto-capture uncaught exceptions
timeout: 5000
});
// Add request middleware (adds req.sniplog helper)
app.use(sniplog.requestMiddleware());
// Your routes here...
// Add error middleware - MUST be after all routes
app.use(sniplog.errorMiddleware());
app.listen(3000);Configuration Options
const sniplog = new SnipLog({
endpoint: 'http://localhost:3000/api/errors', // SnipLog backend URL
projectKey: 'your-api-key', // Project API key
autoCaptureExceptions: true, // Auto-capture process errors (default: true)
enabled: true, // Enable/disable SDK (default: true)
timeout: 5000, // Request timeout in ms (default: 5000)
discordWebhook: 'https://discord.com/api/webhooks/...' // Optional: Discord webhook URL
});Manual Error Capture
app.get('/api/data', async (req, res) => {
try {
const data = await fetchData();
res.json(data);
} catch (err) {
// Capture error with context
req.sniplog.captureError(err, {
userId: req.user?.id,
operation: 'fetch-data',
endpoint: '/api/data'
});
res.status(500).json({ error: 'Failed to fetch data' });
}
});Capture Messages (Non-Errors)
app.post('/api/users', (req, res) => {
req.sniplog.captureMessage('New user registration attempt', {
level: 'info',
email: req.body.email
});
// ... handle registration
});Express Middleware Features
Request Middleware (sniplog.requestMiddleware())
- Adds
req.sniplog.captureError()andreq.sniplog.captureMessage()helpers - Automatically includes request context (method, URL, IP, user-agent)
Error Middleware (sniplog.errorMiddleware())
- Captures all errors that reach Express error handlers
- Includes full request context
- Must be placed AFTER all routes and middleware
Auto-Capture Features
When autoCaptureExceptions: true:
uncaughtExceptionevents are capturedunhandledRejectionevents are captured
Each captured error includes:
- Error message and stack trace
- System info (Node version, OS, hostname)
- Process ID and session ID
- Custom metadata you provide
Example: Complete Express App
See example-express-app.js for a full working example.
To run the example:
# Start the SnipLog backend first (in another terminal)
cd backend
npm start
# Run the example app
cd sdk
node example-express-app.js
# Test error capture
curl http://localhost:4000/test-errorBrowser / Frontend (Coming Soon)
For frontend integration, include browser.js directly:
<script src="/path/to/sniplog-sdk/src/browser.js"></script>
<script>
SnipLog.init({
endpoint: 'http://localhost:3000/api/errors',
projectKey: 'your-project-key'
});
</script>The browser SDK will automatically capture:
window.onerror(script errors)unhandledrejection(promise rejections)console.error(optional)
API Reference
Node.js SDK
new SnipLog(config)
Creates a new SnipLog instance.
Parameters:
config.endpoint(string): SnipLog backend URLconfig.projectKey(string): Your project API keyconfig.autoCaptureExceptions(boolean): Auto-capture process errors (default: true)config.enabled(boolean): Enable/disable SDK (default: true)config.timeout(number): Request timeout in ms (default: 5000)config.discordWebhook(string): Optional Discord webhook URL for real-time notifications
sniplog.captureError(error, metadata)
Manually capture an error.
Parameters:
error(Error): The error objectmetadata(object): Additional context (userId, operation, etc.)
sniplog.captureMessage(message, metadata)
Capture a non-error message/event.
Parameters:
message(string): The messagemetadata(object): Additional context
sniplog.requestMiddleware()
Returns Express middleware that adds req.sniplog helpers.
sniplog.errorMiddleware()
Returns Express error handling middleware. Place after all routes.
Discord Webhook Integration
SnipLog supports real-time error notifications via Discord webhooks. Errors will be sent to both the backend database AND your Discord channel.
Setup Discord Webhook
- Open Discord and go to your server
- Go to Server Settings > Integrations > Webhooks
- Click New Webhook or Create Webhook
- Copy the Webhook URL
SDK Configuration
const sniplog = new SnipLog({
endpoint: 'http://localhost:3000/api/errors',
projectKey: 'your-api-key',
discordWebhook: 'https://discord.com/api/webhooks/1234567890/your-webhook-token'
});Backend Configuration
Alternatively, configure Discord webhook in the backend (all projects will use it):
.env file:
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/1234567890/your-webhook-tokenDiscord Message Format
When an error occurs, you'll receive a Discord message like:
🚨 **expressError**: Cannot read property 'id' of undefined
📅 **Time**: 10/31/2025, 3:45:12 PM
🔗 **URL**: /api/users/123
📍 **Method**: GET
💻 **Host**: production-server
👤 **User**: user-456
Error: Cannot read property 'id' of undefined at getUserProfile (/app/routes/users.js:45:20) at Layer.handle [as handle_request] at next (/app/node_modules/express/lib/router/route.js:137:13)
Features
- ✅ Real-time notifications in Discord
- ✅ Formatted with emojis and markdown
- ✅ Includes error message, stack trace, and metadata
- ✅ Truncated to fit Discord's 2000 character limit
- ✅ Works for both SDK and backend configurations
Development
Project Structure
sdk/
├── src/
│ ├── index.js # Auto-detects environment (Node/browser)
│ ├── node.js # Node.js/Express SDK
│ └── browser.js # Browser SDK (for future frontend use)
├── example-express-app.js
├── package.json
└── README.mdTesting Locally
# Link SDK locally
cd sdk
npm link
# Use in your project
cd your-express-app
npm link sniplog-sdkContributing
We welcome contributions! If you'd like to improve the SDK, please open an issue or submit a pull request.
For details, see our CONTRIBUTING.md guide.
Thanks for helping make SnipLog better!
License
MIT
