-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dd8742
commit 03fc7fe
Showing
17 changed files
with
1,854 additions
and
24,131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-disable space-before-function-paren */ | ||
/* eslint-disable no-console */ | ||
import { createLogMsg } from "./logger"; | ||
(() => { | ||
const _log = console.log; | ||
const _error = console.error; | ||
const _warning = console.warning; | ||
|
||
console.error = function(errMessage) { | ||
_error.apply(console, arguments); | ||
createLogMsg({ | ||
service: "FRONTEND", | ||
what: "Error", | ||
why: errMessage | ||
}); | ||
}; | ||
|
||
console.log = function(logMessage) { | ||
_log.apply(console, arguments); | ||
createLogMsg({ | ||
service: "FRONTEND", | ||
what: "Log", | ||
why: logMessage | ||
}); | ||
}; | ||
|
||
console.warning = function(warnMessage) { | ||
createLogMsg({ | ||
service: "FRONTEND", | ||
what: "Error", | ||
why: warnMessage | ||
}); | ||
_warning.apply(console, arguments); | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import axios from "axios"; | ||
import { createLogger } from "redux-logger"; | ||
import { store } from "./../index"; | ||
const { | ||
NODE_ENV, | ||
REACT_APP_LOGGING, | ||
REACT_APP_LOG_LEVEL, | ||
REACT_APP_LOGGING_SERVICE_HOST, | ||
REACT_APP_LOGGING_SERVICE_PORT, | ||
REACT_APP_LOGGING_SERVICE_HOST_SSL | ||
} = process.env; | ||
|
||
let instance = undefined; | ||
const logMessages = []; | ||
const getToken = () => (store ? store.getState().toJS().login.jwt : ""); | ||
const getUserId = () => (store ? store.getState().toJS().login.id : ""); | ||
|
||
const createConnection = () => { | ||
if (REACT_APP_LOGGING === false) return; | ||
// SSL musst be enabled when using logger in production | ||
if (NODE_ENV !== "developement" && REACT_APP_LOGGING_SERVICE_HOST_SSL === "false" && REACT_APP_LOGGING === "true") { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
"Seems you are using TruBudget in production with logging enabled but without SSL! Enable SSL for Frontend-Logging to proceed!" | ||
); | ||
} | ||
// Build url | ||
instance = axios.create(); | ||
instance.defaults.baseURL = `${ | ||
REACT_APP_LOGGING_SERVICE_HOST_SSL ? "http://" : "https://" | ||
}${REACT_APP_LOGGING_SERVICE_HOST}:${REACT_APP_LOGGING_SERVICE_PORT}`; | ||
|
||
setInterval(pushLogToServer, 1000 * 10); | ||
}; | ||
|
||
const setToken = () => { | ||
let t = getToken(); | ||
instance.defaults.headers.common["Authorization"] = t ? `Bearer ${t}` : ""; | ||
}; | ||
|
||
const logger = createLogger({ | ||
timestamp: true, | ||
logErrors: true, | ||
predicate: (getState, action) => predicate(getState, action), | ||
stateTransformer: s => stateTransformer(s), | ||
diff: true, | ||
errorTransformer: error => errorTransformer(error) | ||
}); | ||
const stateTransformer = s => s; | ||
|
||
const predicate = (getState, action) => { | ||
createLogMsg({ | ||
service: "FRONTEND", | ||
what: "Trace", | ||
why: { | ||
action: action, | ||
prevState: stateTransformer(getState()) | ||
} | ||
}); | ||
//In trace mode print to console | ||
if (REACT_APP_LOG_LEVEL === "trace" && REACT_APP_LOGGING === "true") return true; | ||
return false; | ||
}; | ||
|
||
const errorTransformer = error => { | ||
createLogMsg({ | ||
service: "FRONTEND", | ||
what: "Error", | ||
why: error | ||
}); | ||
return error; | ||
}; | ||
const pushLogToServer = async () => { | ||
if (instance && logMessages.length > 0) { | ||
if ( | ||
instance.defaults.headers.common["Authorization"] === "" || | ||
instance.defaults.headers.common["Authorization"] === undefined | ||
) | ||
setToken(); | ||
await instance.post("/api", { logMessages: logMessages }).catch(ignore => ignore); | ||
while (logMessages.length) { | ||
logMessages.pop(); | ||
} | ||
} | ||
}; | ||
|
||
export const createLogMsg = async log => { | ||
if (REACT_APP_LOGGING === "false") return; | ||
const msg = { | ||
...log, | ||
when: new Date().toString(), | ||
who: getUserId() | ||
}; | ||
logMessages.push(msg); | ||
}; | ||
|
||
createConnection(); | ||
export default logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
LOGGER_PORT=3001 | ||
API_HOST=localhost | ||
API_PORT=8080 | ||
LOG_LEVEL=trace | ||
NODE_ENV=development |
Oops, something went wrong.