Socket
Socket
Sign inDemoInstall

log4js-knex

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

log4js-knex - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

__mocks__/knex.js

119

lib/log4js-knex.js

@@ -16,77 +16,66 @@ 'use strict';

function knexAppender(config) {
function knexAppender(config, layouts) {
if (! config.knex) {
throw new Error('knex.js connection parameters are missing');
}
if (! config.knex) {
throw new Error('knex.js connection parameters are missing');
}
var layout = config.layout || log4js.layouts.messagePassThroughLayout;
var tableName = config.table || 'log';
var knex = typeof config.knex === 'object' ? Knex(config.knex) : config.knex;
var layout = null;
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
} else {
layout = layouts.messagePassThrough;
}
// Solution to the performance issues of testing for an existing table before logging
// is to write the damn row and check after. This is probably (definitely) a bit of a
// risk for portability, but loggers aren't that good for one-off configuration, and
// reaally, it should be a decent solution.
var tableName = config.table || 'log';
var knex = typeof config.knex === 'object' ? Knex(config.knex) : config.knex;
// The naive logic is as follows. If we get an error on insert -- any error -- then we
// try to add a table. If we fail to do that, throw the original error. If we succeed,
// try a second time to add the row, but this time without any safety net.
// Solution to the performance issues of testing for an existing table before logging
// is to write the damn row and check after. This is probably (definitely) a bit of a
// risk for portability, but loggers aren't that good for one-off configuration, and
// reaally, it should be a decent solution.
// Logically, there could be a timing issue here on startup. If two processes attempt
// to log, both fail, and both attempt to create, one will signal an error.
// The naive logic is as follows. If we get an error on insert -- any error -- then we
// try to add a table. If we fail to do that, throw the original error. If we succeed,
// try a second time to add the row, but this time without any safety net.
function createTableIfNeeded(loggingEvent, originalError) {
return knex.schema.createTable(tableName, function (table) {
table.increments();
table.timestamp('time').notNullable();
table.string('data', 4096).notNullable();
table.integer('rank').notNullable();
table.string('level', 12).notNullable();
table.string('category', 64).notNullable();
}).exec(function(err, data) {
if (err) {
throw originalError;
} else {
writeEvent(loggingEvent, originalError);
}
});
}
// Logically, there could be a timing issue here on startup. If two processes attempt
// to log, both fail, and both attempt to create, one will signal an error.
function writeEvent(loggingEvent, originalError) {
if (Object.prototype.toString.call(loggingEvent.data[0]) === '[object String]') {
loggingEvent.data = layout(loggingEvent);
} else if (loggingEvent.data.length === 1) {
loggingEvent.data = loggingEvent.data[0];
}
function createTableIfNeeded(loggingEvent, originalError) {
return knex.schema.createTable(tableName, function (table) {
table.increments();
table.timestamp('time').notNullable();
table.string('data', 4096).notNullable();
table.integer('rank').notNullable();
table.string('level', 12).notNullable();
table.string('category', 64).notNullable();
})
.catch((err) => {
throw originalError;
})
.then(() => writeEvent(loggingEvent, originalError));
}
return knex(tableName).insert({
time: loggingEvent.startTime,
data: loggingEvent.data,
rank: loggingEvent.level.level,
level: loggingEvent.level.levelStr,
category: loggingEvent.logger.category
}).exec(function(err, data) {
if (err) {
if (originalError) {
throw originalError
} else {
createTableIfNeeded(loggingEvent, err);
}
}
});
};
function writeEvent(loggingEvent, originalError) {
const formatted = layout(loggingEvent);
return knex(tableName).insert({
time: loggingEvent.startTime,
data: formatted,
rank: loggingEvent.level.level,
level: loggingEvent.level.levelStr,
category: loggingEvent.logger.category
})
.catch(function(err) {
if (originalError) {
throw originalError;
} else {
return createTableIfNeeded(loggingEvent, err);
}
});
};
return writeEvent;
return writeEvent;
}
function configure (config) {
if (config.layout) {
config.layout = log4js.layouts.layout(config.layout.type, config.layout);
}
return knexAppender(config);
}
exports.appender = knexAppender;
exports.configure = configure;
exports.configure = knexAppender;
{
"name": "log4js-knex",
"version": "0.1.0",
"version": "0.2.0",
"description": "A Knex.js backend for log4js, with sqlite3/mysql support",

@@ -12,20 +12,39 @@ "keywords": [

],
"repository": "git@github.com:log4js-knex.git",
"repository": {
"type": "git",
"url": "git@github.com:morungos/log4js-knex.git"
},
"author": "Stuart Watt <stuart@morungos.com>",
"homepage": "https://github.com/morungos/log4js-knex",
"engines": {
"node": ">= 0.10.0"
"license": "MIT",
"main": "./lib/log4js-knex",
"dependencies": {},
"peerDependencies": {
"log4js": "3.x",
"knex": "^0.13"
},
"main": "./index.js",
"dependencies": {
"knex": "^0.7.3",
"log4js": "^0.6.21"
},
"devDependencies": {
"chai": "^1.10.0",
"sqlite3": "^3.0.4"
"jest": "^23.6.0",
"log4js": "^3.0.6"
},
"scripts": {
"test": "mocha test/runner.js --reporter spec"
"test": "jest",
"test-watch": "jest --watch",
"coverage": "jest --coverage"
},
"jest": {
"moduleFileExtensions": [
"js",
"json"
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
],
"modulePathIgnorePatterns": [
"<rootDir>/build/"
],
"collectCoverageFrom": [
"lib/**/*.js"
]
}
}
}
log4js-knex
===========
This is a node [log4js](https://github.com/nomiddlename/log4js-node) appender that uses [knex](http://knexjs.org/)
as a database connection interface.
This is a node [log4js](https://github.com/nomiddlename/log4js-node) appender that
uses [knex](http://knexjs.org/) as a database connection interface.

@@ -36,5 +36,5 @@ You can use it either with a set of connection values:

The default table name is `log`, and if there's an error when writing, the appender will attempt
to create the table before having a second attempt at writing. This should create the table for
you if it doesn't already exist.
The default table name is `log`, and if there's an error when writing, the appender
will attempt to create the table before having a second attempt at writing. This
should create the table for you if it doesn't already exist.

@@ -51,3 +51,3 @@ Author

Copyright (c) 2014 Stuart Watt
Copyright (c) 2014-2018 Stuart Watt

@@ -54,0 +54,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc