Skip to content

Commit

Permalink
chore: upgrade and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-ua committed Dec 28, 2024
1 parent 573b7d3 commit 4956fed
Show file tree
Hide file tree
Showing 14 changed files with 2,399 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
coverage/
dist/
.nyc_output/
52 changes: 52 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module.exports = {
env: {
node: true,
es2024: true,
commonjs: true,
},
parserOptions: {
ecmaVersion: 2024,
sourceType: 'commonjs',
},
extends: [
'eslint:recommended',
],
rules: {
'indent': ['error', 2],
'linebreak-style': ['error', 'unix'],
'quotes': ['error', 'single'],
'semi': ['error', 'always'],
'no-unused-vars': ['error', { 'argsIgnorePattern': '^_' }],
'no-console': ['error', { allow: ['warn', 'error'] }],
'no-constant-condition': ['error', { checkLoops: false }],
'eqeqeq': ['error', 'always'],
'curly': ['error', 'all'],
'comma-dangle': ['error', {
'arrays': 'always-multiline',
'objects': 'always-multiline',
'imports': 'always-multiline',
'exports': 'always-multiline',
'functions': 'never',
}],
},
overrides: [
{
files: ['test/**/*.js'],
env: {
node: true,
commonjs: true,
es2024: true,
},
rules: {
'no-console': 'off',
},
},
],
globals: {
'__dirname': 'readonly',
'process': 'readonly',
'require': 'readonly',
'module': 'readonly',
'exports': 'readonly',
},
};
62 changes: 62 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const esbuild = require('esbuild');
const { readFile } = require('fs/promises');
const path = require('path');

async function getBanner() {
const pkg = JSON.parse(
await readFile(path.join(__dirname, 'package.json'), 'utf8')
);
return `/**
* @license
* ${pkg.name} v${pkg.version}
* Copyright (c) ${new Date().getFullYear()} ${pkg.author}
* Licensed under the ${pkg.license} license
*/`;
}

async function build() {
const banner = await getBanner();

try {
await esbuild.build({
entryPoints: ['./index.js'],
bundle: true,
minify: true,
platform: 'node',
target: 'node20',
outfile: 'dist/log4js-pg-appender.min.js',
banner: {
js: banner,
},
external: ['pg'], // Don't bundle external dependencies
format: 'cjs',
metafile: true,
sourcemap: true,
});

// Build metadata for analysis
const metadata = await esbuild.build({
entryPoints: ['./index.js'],
bundle: true,
minify: true,
platform: 'node',
target: 'node20',
external: ['pg'],
format: 'cjs',
metafile: true,
write: false,
});

// Output build analysis
const text = await esbuild.analyzeMetafile(metadata.metafile);
// eslint-disable-next-line no-console
console.log('Build analysis:', text);

} catch (error) {
// eslint-disable-next-line no-console
console.error('Build failed:', error);
process.exit(1);
}
}

build();
8 changes: 8 additions & 0 deletions dist/log4js-pg-appender.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions dist/log4js-pg-appender.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion lib/appender.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const db = require ('./db');
const writerFactory = require ('./writer');

function validateConfig(config) {
if (!config) {throw new Error('config is required');}
if (!config.host) {throw new Error('host is required');}
if (!config.database) {throw new Error('database is required');}
if (!config.user) {throw new Error('user is required');}
if (!config.password) {throw new Error('password is required');}
if (!config.tableName) {throw new Error('tableName is required');}
}

function createPgAppender (config) {
validateConfig(config);
const client = db.connect (config);
const writer = writerFactory (client, config.tableName);
return loggingEvent => {
Expand All @@ -11,7 +21,7 @@ function createPgAppender (config) {
categoryName,
JSON.stringify (data),
level.levelStr,
JSON.stringify (context)
context ? JSON.stringify (context) : null
).catch (error => {
// eslint-disable-next-line no-console
console.error (error.stack);
Expand Down
8 changes: 7 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ const {Client} = require ('pg');
let db;
module.exports = {
connect (options) {
if (db) {
return db;
}
db = new Client (options);
db.connect ();
return db;
},
disconnect () {
db.end ();
if (db) {
db.end();
db = null;
}
},
};
10 changes: 5 additions & 5 deletions lib/writer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = (client, tableName = 'Logs') => {
const query = {
name: 'log4js-pg-appender-add-record',
text: `INSERT INTO "${ tableName }" ("startTime", "categoryName", "data", "level", "context") VALUES($1, $2, $3, $4, $5)`,
};
return (...values) => client.query({ ...query, values});
const query = {
name: 'log4js-pg-appender-add-record',
text: `INSERT INTO "${ tableName }" ("startTime", "categoryName", "data", "level", "context") VALUES($1, $2, $3, $4, $5)`,
};
return (...values) => client.query({ ...query, values});
};
Loading

0 comments on commit 4956fed

Please sign in to comment.