Skip to content

Commit

Permalink
add enabling/disabling loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
stCarolas committed Mar 10, 2024
1 parent a97f3c7 commit 8c1e9ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Config {
paymentWidgetCommands: string;
mediaWidgetCommands: string;
};
loglevel: string;
}

async function config(recipientId: string): Promise<Config> {
Expand All @@ -27,6 +28,7 @@ async function config(recipientId: string): Promise<Config> {
paymentWidgetCommands: "",
mediaWidgetCommands: "",
},
loglevel: "error"
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import DonatersTopList from "./components/DonatersTopList/DonatersTopList";
import ConfigurationPage from "./components/ConfigurationPage/ConfigurationPage";
import "./index.css";
import { config } from "./config";
import { log } from "./logging";
import { log, setLoglevel } from "./logging";
import auth from "./auth";
import axios from "axios";
import Login from "./components/Login/Login";
Expand Down Expand Up @@ -44,6 +44,7 @@ async function widgetSettingsLoader({
}

const conf = await config(recipientId);
setLoglevel(conf.loglevel);
log.debug(`Configuration: ${JSON.stringify(conf)}`);
const widgetId = params.widgetId;

Expand Down
28 changes: 25 additions & 3 deletions src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import pino from "pino";

function valueOf(level: string):number {
switch(level){
case 'error':
return 0;
case 'warn':
return 1;
case 'info':
return 2;
case 'debug':
return 3;
default:
return 0;
}
}

// todo локально работает, надо сделать чтобы по флагу/команде включалось в проде
const send = async function (level, logEvent) {
const send = async function (level: string, logEvent) {
if (valueOf(level) > valueOf(loglevel)) {
return;
}
const url = `${process.env.REACT_APP_LOG_API_ENDPOINT}/logs/${localStorage.getItem("login")}`;

try {
Expand All @@ -17,6 +33,12 @@ const send = async function (level, logEvent) {
} catch (Exception) {}
};

let loglevel = "error";

function setLoglevel(level: string){
loglevel = level;
}

const log = pino({
browser: {
serialize: true,
Expand All @@ -28,4 +50,4 @@ const log = pino({
});
log.level = process.env.REACT_APP_PINO_LOG_LEVEL || "debug";

export { log };
export { log, setLoglevel };

0 comments on commit 8c1e9ae

Please sign in to comment.