-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1283 from MarcelRobitaille/1275-add-logging
Add logging to diagnose bugs in production
- Loading branch information
Showing
12 changed files
with
140 additions
and
6 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
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,21 @@ | ||
# Debugging in production through logging | ||
|
||
Sometimes, it is necessary to debug problems in production | ||
where it is not possible to use a debugger or other developer tools to find the | ||
source of the problem. | ||
For example, if a user reports an issue that cannot be reproduced locally, | ||
you can instruct them to enable logging to narrow down the source of the issue. | ||
|
||
To enable logging, a user only has to run the following before loading the app/refreshing the page: | ||
```js | ||
localStorage.setItem('COOKBOOK_LOGGING_ENABLED', 'true') | ||
``` | ||
|
||
This will automatically be reset after 30 minutes so verbose logging is not enabled permanently for the user. | ||
|
||
The log level is also configurable. For example: | ||
```js | ||
localStorage.setItem('COOKBOOK_LOGGING_LEVEL', 'debug') | ||
``` | ||
|
||
The documentation for the logging library used is available [here](https://www.npmjs.com/package/vuejs-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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ export default { | |
}, | ||
}, | ||
mounted() { | ||
this.$log.info("AppIndex mounted") | ||
this.loadAll() | ||
}, | ||
methods: { | ||
|
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
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,68 @@ | ||
// TODO: Switch to vuejs3-logger when we switch to Vue 3 | ||
import VueLogger from "vuejs-logger" | ||
import moment from "@nextcloud/moment" | ||
|
||
const DEFAULT_LOG_LEVEL = "info" | ||
// How many minutes the logging configuration is valid for | ||
const EXPIRY_MINUTES = 30 | ||
// localStorage keys | ||
const KEY_ENABLED = "COOKBOOK_LOGGING_ENABLED" | ||
const KEY_EXPIRY = "COOKBOOK_LOGGING_EXPIRY" | ||
const KEY_LOG_LEVEL = "COOKBOOK_LOGGING_LEVEL" | ||
|
||
// Check if the logging configuration in local storage has expired | ||
// | ||
// Since the expiry entry is added by us after the first run where | ||
// the enabled entry is detected, this only checks if it has been EXPIRY_MINUTES | ||
// since the first run, not EXPIRY_MINUTES since the user added the entry | ||
// This is a reasonable comprimise to simplify what the user has to do to enable | ||
// logging. We don't want them to have to setup the expiry as well | ||
const isExpired = (timestamp) => { | ||
if (timestamp === null) { | ||
return false | ||
} | ||
|
||
return moment().isAfter(parseInt(timestamp, 10)) | ||
} | ||
|
||
const isEnabled = () => { | ||
const DEFAULT = false | ||
const userValue = localStorage.getItem(KEY_ENABLED) | ||
const expiry = localStorage.getItem(KEY_EXPIRY) | ||
|
||
// Detect the first load after the user has enabled logging | ||
// Set the expiry so the logging isn't enabled forever | ||
if (userValue !== null && expiry === null) { | ||
localStorage.setItem( | ||
KEY_EXPIRY, | ||
moment().add(EXPIRY_MINUTES, "m").valueOf() | ||
) | ||
} | ||
|
||
if (isExpired(expiry)) { | ||
localStorage.removeItem(KEY_ENABLED) | ||
localStorage.removeItem(KEY_EXPIRY) | ||
|
||
return DEFAULT | ||
} | ||
|
||
// Local storage converts everything to string | ||
// Use JSON.parse to transform "false" -> false | ||
return JSON.parse(userValue) ?? DEFAULT | ||
} | ||
|
||
export default function setupLogging(Vue) { | ||
const logLevel = localStorage.getItem(KEY_LOG_LEVEL) ?? DEFAULT_LOG_LEVEL | ||
|
||
Vue.use(VueLogger, { | ||
isEnabled: isEnabled(), | ||
logLevel, | ||
stringifyArguments: false, | ||
showLogLevel: true, | ||
showMethodName: true, | ||
separator: "|", | ||
showConsoleColors: true, | ||
}) | ||
|
||
Vue.$log.info(`Setting up logging with log level ${logLevel}`) | ||
} |
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