log4js-knex
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
16389
8
275
1
1
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedarr-diff@2.0.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarray-unique@0.2.1(transitive)
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedbraces@1.8.5(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcircular-json@0.5.9(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addeddate-format@1.2.0(transitive)
+ Addeddebug@2.6.93.2.7(transitive)
+ Addeddetect-file@0.1.0(transitive)
+ Addedexpand-brackets@0.1.5(transitive)
+ Addedexpand-range@1.8.2(transitive)
+ Addedexpand-tilde@1.2.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextglob@0.3.2(transitive)
+ Addedfilename-regex@2.0.1(transitive)
+ Addedfill-range@2.2.4(transitive)
+ Addedfindup-sync@0.4.3(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfor-own@0.1.5(transitive)
+ Addedfs-exists-sync@0.1.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgeneric-pool@2.5.4(transitive)
+ Addedglob-base@0.3.0(transitive)
+ Addedglob-parent@2.0.0(transitive)
+ Addedglobal-modules@0.2.3(transitive)
+ Addedglobal-prefix@0.1.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhomedir-polyfill@1.0.3(transitive)
+ Addedini@1.3.8(transitive)
+ Addedinterpret@0.6.6(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-dotfile@1.0.3(transitive)
+ Addedis-equal-shallow@0.1.3(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedis-extglob@1.0.0(transitive)
+ Addedis-glob@2.0.1(transitive)
+ Addedis-number@2.1.04.0.0(transitive)
+ Addedis-posix-bracket@0.1.1(transitive)
+ Addedis-primitive@2.0.0(transitive)
+ Addedis-windows@0.2.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedisobject@2.1.0(transitive)
+ Addedkind-of@3.2.26.0.3(transitive)
+ Addedknex@0.13.0(transitive)
+ Addedliftoff@2.2.5(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlog4js@3.0.6(transitive)
+ Addedmath-random@1.0.4(transitive)
+ Addedmicromatch@2.3.11(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednormalize-path@2.1.1(transitive)
+ Addedobject.omit@2.0.1(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedparse-glob@3.0.4(transitive)
+ Addedparse-passwd@1.0.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpg-connection-string@0.1.3(transitive)
+ Addedpreserve@0.2.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedrandomatic@3.1.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedrechoir@0.6.2(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)
+ Addedregex-cache@0.4.4(transitive)
+ Addedremove-trailing-separator@1.1.0(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedresolve-dir@0.1.1(transitive)
+ Addedrfdc@1.4.1(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedstreamroller@0.7.0(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedv8flags@2.1.1(transitive)
+ Addedwhich@1.3.1(transitive)
- Removedknex@^0.7.3
- Removedlog4js@^0.6.21
- Removedansi-regex@0.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
- Removedbluebird@2.11.0(transitive)
- Removedchalk@0.5.1(transitive)
- Removedextend@1.3.0(transitive)
- Removedfindup-sync@0.1.3(transitive)
- Removedgeneric-pool-redux@0.1.0(transitive)
- Removedglob@3.2.11(transitive)
- Removedhas-ansi@0.1.0(transitive)
- Removedinterpret@0.3.10(transitive)
- Removedknex@0.7.6(transitive)
- Removedliftoff@0.13.6(transitive)
- Removedlodash@2.4.2(transitive)
- Removedlog4js@0.6.38(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedresolve@1.0.0(transitive)
- Removedsemver@4.3.6(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedstrip-ansi@0.3.0(transitive)
- Removedsupports-color@0.2.0(transitive)