Skip to content

Commit

Permalink
Add log filter
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.zhemerenko committed Feb 22, 2024
1 parent 9a146be commit 4c2a8e6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '20.x'
node-version: '21.x'
registry-url: 'https://registry.npmjs.org'
- run: yarn && yarn build
- run: yarn publish
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
21
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spryrocks/capacitor-ionic-core-plugin",
"version": "5.7.0-alpha.0",
"version": "5.8.0-alpha.3",
"description": "Ionic plugin core capacitor",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down
49 changes: 32 additions & 17 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createLoggerFactory,
GlobalData,
ILoggerObserver,
LogData,
LoggerObserver,
prepareLogData,
} from './logger';
Expand All @@ -32,6 +33,7 @@ type LogEvent = {

export interface ICapacitorPlugin {
get logObserver(): ILoggerObserver;
setLogLevels(logLevels: LogLevel[] | undefined): Promise<void>;
}

export type PluginOptions<TDefinitions extends IDefinitions> = {
Expand All @@ -56,6 +58,8 @@ export abstract class CapacitorPlugin<

private readonly _loggerFactory: ILoggerFactory<GlobalData>;

private logLevels: LogLevel[] | undefined;

protected abstract readonly mappers: TMappers;

// noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
Expand Down Expand Up @@ -136,6 +140,7 @@ export abstract class CapacitorPlugin<
) {
try {
this.proxy.addListener(name, (event) => {
// eslint-disable-next-line no-console
console.log('CapacitorPlugin', 'event received', name, event);
listener(event as TListener);
});
Expand Down Expand Up @@ -164,23 +169,23 @@ export abstract class CapacitorPlugin<
}

private processLogEventReceived(event: LogEvent) {
this._logNotifiers.notify(
prepareLogData({
data: {
tag: event.tag,
message: event.message,
params: event.params,
level: event.level,
error: undefined,
errorLevel: ErrorLevel.Medium,
},
globalData: {
plugin: this.options.pluginLogName,
action: event.action,
isNative: true,
},
}),
);
const data = prepareLogData({
data: {
tag: event.tag,
message: event.message,
params: event.params,
level: event.level,
error: undefined,
errorLevel: ErrorLevel.Medium,
},
globalData: {
plugin: this.options.pluginLogName,
action: event.action,
isNative: true,
},
});
if (!this.testLog(data)) return;
this._logNotifiers.notify(data);
}

// private isPluginAvailableInCapacitor() {
Expand Down Expand Up @@ -208,4 +213,14 @@ export abstract class CapacitorPlugin<
public get proxy() {
return this.options.proxy;
}

public async setLogLevels(logLevels: LogLevel[] | undefined) {
this.logLevels = logLevels;
}

private testLog(data: LogData) {
const logLevels = this.logLevels;
if (!logLevels) return true;
return logLevels.includes(data.level);
}
}

0 comments on commit 4c2a8e6

Please sign in to comment.