Skip to content

Commit

Permalink
Merge pull request #71 from atomicsamurai/custom-noise-filter
Browse files Browse the repository at this point in the history
Custom noise filter
  • Loading branch information
atomicsamurai authored Oct 4, 2022
2 parents 8c59a74 + f82dfc6 commit 19b612d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- \#70: Added ability to create custom logging noise filters

## [0.15.0] - 2022-10-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"main": "esm/app.js",
"scripts": {
"test": "npx tsc && node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:list": "node --experimental-vm-modules node_modules/jest/bin/jest.js --listTests",
"test:e2e": "node --experimental-vm-modules node_modules/jest/bin/jest.js --runInBand e2e",
"test:e2e:list": "node --experimental-vm-modules node_modules/jest/bin/jest.js --listTests e2e",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/logging/logs-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { provisionCreds, getLogSources } = Log;
const { getConnectionProfile, saveConnectionProfile } = ConnectionProfile;
const { getTokens } = Authenticate;

const program = new Command('frodo journey list');
const program = new Command('frodo logs list');
program
.description('List available ID Cloud log sources.')
.helpOption('-h, --help', 'Help')
Expand Down
12 changes: 10 additions & 2 deletions src/cli/logging/logs-tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
state,
} from '@rockcarver/frodo-lib';
import * as common from '../cmd_common.js';
import * as config from '../../utils/Config.js';

const { provisionCreds, tailLogs, resolveLevel } = Log;
const { getConnectionProfile, saveConnectionProfile } = ConnectionProfile;
const { getTokens } = Authenticate;

const program = new Command('frodo journey tail');
const program = new Command('frodo logs tail');
program
.description('Tail Identity Cloud logs.')
.helpOption('-h, --help', 'Help')
Expand All @@ -33,6 +34,12 @@ Following values are possible (values on the same line are equivalent): \
.addOption(
new Option('-t, --transaction-id <txid>', 'Filter by transactionId')
)
.addOption(
new Option('-d, --defaults', 'Use default logging noise filters').default(
false,
`Use custom logging noise filters defined in ${config.getConfigPath()}/${config.FRODO_LOG_NOISEFILTER_FILENAME}`
)
)
.action(async (host, user, password, options, command) => {
let credsFromParameters = true;
state.default.session.setTenant(host);
Expand Down Expand Up @@ -78,7 +85,8 @@ Following values are possible (values on the same line are equivalent): \
command.opts().sources,
resolveLevel(command.opts().level),
command.opts().transactionId,
null
null,
config.getNoiseFilters(options.defaults)
);
});

Expand Down
52 changes: 52 additions & 0 deletions src/utils/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from 'fs';
import os from 'os';
import { Log } from '@rockcarver/frodo-lib';

import { printMessage } from './Console';

const { defaultNoiseFilter } = Log;

export const FRODO_CONFIG_PATH_KEY = 'FRODO_CONFIG_PATH';
export const FRODO_LOG_NOISEFILTER_FILENAME = 'LoggingNoiseFilter.json';

export function getConfigPath(): string {
return process.env[FRODO_CONFIG_PATH_KEY] || `${os.homedir()}/.frodo`;
}

function getCustomNoiseFilters(): Array<string> {
const filename = `${getConfigPath()}/${FRODO_LOG_NOISEFILTER_FILENAME}`;
let noiseFilter = [];
try {
const data = fs.readFileSync(filename, 'utf8');
noiseFilter = JSON.parse(data);
} catch (e) {
printMessage(`Error reading ${filename} (${e.message})`, 'error');
}
return noiseFilter;
}

export function getNoiseFilters(defaults: boolean): Array<string> {
const filename = `${getConfigPath()}/${FRODO_LOG_NOISEFILTER_FILENAME}`;
if(defaults) {
printMessage(`Using default logging noise filters.`, 'info');
return defaultNoiseFilter();
}
let noiseFilter = getCustomNoiseFilters();
if (noiseFilter.length > 0) {
printMessage(`Using custom noise filters from ${filename}`, 'info');
} else {
printMessage(`No custom noise filters defined. Using defaults.`, 'info');
noiseFilter = defaultNoiseFilter();
try {
fs.writeFileSync(filename, JSON.stringify(noiseFilter, null, 2));
printMessage(
`The default filters were saved in ${filename}. You can change the filters as needed.`,
'info'
);
} catch (e) {
printMessage(`Error creating noise filter configuration with default values.`, 'error');
}
}
return noiseFilter;
}

2 changes: 1 addition & 1 deletion test/client_cli/en/logs-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { stdout } = await exec(CMD);
test("CLI help interface for 'logs list' Usage should be expected english", async () => {
// Arrange
const expected = `
Usage: frodo journey list [options] <host> [user] [password]
Usage: frodo logs list [options] <host> [user] [password]
`.trim();
// Act
const testLine = stdout
Expand Down
12 changes: 7 additions & 5 deletions test/client_cli/en/logs-tail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { stdout } = await exec(CMD);
test("CLI help interface for 'logs tail' Usage should be expected english", async () => {
// Arrange
const expected = `
Usage: frodo journey tail [options] <host> [user] [password]
Usage: frodo logs tail [options] <host> [user] [password]
`.trim();
// Act
const testLine = stdout
Expand Down Expand Up @@ -113,9 +113,9 @@ test("CLI help interface 'list option -k, --insecure' description should be expe

test("CLI help interface 'list option -c, --sources <sources>' description should be expected english multiline", async () => {
// Arrange
const expected = collapseWhitespace(`
-c, --sources <sources> Comma separated list of log sources (default: Log everything) -l, --level <level> Set log level filter. You can specify the level as a number or a string. Following values are possible (values on the same line are equivalent): 0, SEVERE, FATAL, or ERROR 1, WARNING, WARN or CONFIG 2, INFO or INFORMATION 3, DEBUG, FINE, FINER or FINEST 4 or ALL (default: SEVERE,ERROR,FATAL) -t, --transaction-id <txid> Filter by transactionId
`);
// const expected = collapseWhitespace(`
// -c, --sources <sources> Comma separated list of log sources (default: Log everything) -l, --level <level> Set log level filter. You can specify the level as a number or a string. Following values are possible (values on the same line are equivalent): 0, SEVERE, FATAL, or ERROR 1, WARNING, WARN or CONFIG 2, INFO or INFORMATION 3, DEBUG, FINE, FINER or FINEST 4 or ALL (default: SEVERE,ERROR,FATAL) -t, --transaction-id <txid> Filter by transactionId -d, --defaults Use default logging noise filters (default: Use custom logging noise filters defined in (\w|\/|\.)+/gm)
// `);
// Act
const testLine = collapseWhitespace(
crudeMultilineTakeUntil(
Expand All @@ -126,5 +126,7 @@ test("CLI help interface 'list option -c, --sources <sources>' description shoul
);

// Assert
expect(testLine).toBe(expected);
expect(testLine).toMatch(
/-c, --sources <sources> Comma separated list of log sources \(default: Log everything\) -l, --level <level> Set log level filter\. You can specify the level as a number or a string\. Following values are possible \(values on the same line are equivalent\): 0, SEVERE, FATAL, or ERROR 1, WARNING, WARN or CONFIG 2, INFO or INFORMATION 3, DEBUG, FINE, FINER or FINEST 4 or ALL \(default: SEVERE,ERROR,FATAL\) -t, --transaction-id <txid> Filter by transactionId -d, --defaults Use default logging noise filters \(default: Use custom logging noise filters defined in (\w|\/|\.)+\)/gm
);
});

0 comments on commit 19b612d

Please sign in to comment.