Registry / testing / log4js-node-mongodb

log4js-node-mongodb

JSON →
library2.2.1jsnpmunverified

A log appender for log4js-node that writes log events to MongoDB. Version 2.2.1 is the latest stable release, compatible with log4js 1.x. It stores logs in a configurable collection with fields for timestamp, data, level, and category. Supports connection options and can log strings or objects. Compared to other MongoDB appenders, it is lightweight and designed specifically for the log4js ecosystem. The package is maintained by Litixsoft but updates are infrequent.

npm install log4js-node-mongodb
INSTALL
IMPORT
SIG · LOG4JS-NODE-MONGOD
L
log4js-node-mongodb
testingjavascriptv2.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

appender
const mongoAppender = require('log4js-node-mongodb'); log4js.addAppender(mongoAppender.appender({...}), 'category');
import { appender } from 'log4js-node-mongodb'; log4js.addAppender(appender({...}), 'category');
Package is CommonJS-only, not ESM. The default export is an object with the appender method.
configure
log4js.configure({ appenders: [{ type: 'log4js-node-mongodb', connectionString: '...', category: '...' }] });
log4js.configure({ appenders: [{ type: 'mongo', ... }] });
The type must be the package name 'log4js-node-mongodb', not an alias.
default export
const mongoAppender = require('log4js-node-mongodb');
const mongoAppender = require('log4js-node-mongodb').default;
No default export; the require returns the module object.

Shows how to integrate the MongoDB appender with log4js, create a logger, and log a string and an object.

const log4js = require('log4js'); const mongoAppender = require('log4js-node-mongodb'); log4js.addAppender( mongoAppender.appender({ connectionString: process.env.MONGO_URL ?? 'localhost:27017/logs', collectionName: 'app_logs' }), 'myApp' ); const logger = log4js.getLogger('myApp'); logger.info('Application started'); logger.error({ error: 'Something went wrong', code: 500 });
Debug
Known issues
breakingIncompatible with log4js v2+; this package only supports log4js v1.x.
fix
Downgrade log4js to v1.x or find an alternative appender that supports log4js v2.
affects: >=2.0.0
deprecatedPackage uses the old MongoDB driver API; may not work with MongoDB 5+ or newer driver versions.
fix
Consider using a modern MongoDB appender or update the connection options for newer drivers.
affects: >=5.0.0
gotchaThe connectionString must include the database name (e.g., 'localhost:27017/logs'), not just the host.
fix
Append '/yourDatabaseName' to the connection string.
affects: *
gotchaWhen using the configure method, the appender type must be exactly 'log4js-node-mongodb', not an abbreviation.
fix
Set type to 'log4js-node-mongodb'.
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'log4js-node-mongodb'
npm package not installed or typo in require path
fix
Run npm install log4js-node-mongodb and verify the package name in require()
TypeError: mongoAppender.appender is not a function
Incorrect import style (e.g., using ES6 import) or require returns undefined
fix
Use const mongoAppender = require('log4js-node-mongodb');. Do not use destructuring.
MongoError: Authentication failed
Missing credentials in connection string or connectionOptions
fix
Include username:password@host in connectionString or set connectionOptions.auth
TypeError: log4js.addAppender is not a function
Using log4js v2+ which does not have addAppender
fix
Downgrade log4js to v1.x or use configure method instead.
Upgrade
Version history
2.2.1latest on npm
Audit
Dependencies
log4jsrequiredPeer dependency for log4js appender integration
mongodbrequiredMongoDB driver for database connections
Agent activity
6 hits · last 30 days
node
6
Resources
log4js-node-mongodb — npm install log4js-node-mongodb · libregistry