feat: add logging setup for tauri desktop app (#397)

This commit is contained in:
Jonas Kruckenberg 2022-12-12 21:16:45 +01:00 committed by GitHub
parent d856abef9d
commit 85f619ea82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 3 deletions

View file

@ -1,4 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
setupLogging()
setupPageHeader() setupPageHeader()
await setupI18n() await setupI18n()
provideGlobalCommands() provideGlobalCommands()

21
composables/log.ts Normal file
View file

@ -0,0 +1,21 @@
import * as log from 'tauri-plugin-log-api'
// When running inside Tauri, catch all logs from 3rd party packages and direct them to the unified logging stream
export function setupLogging() {
if (import.meta.env.TAURI_PLATFORM) {
// eslint-disable-next-line no-global-assign
console = {
...console,
trace: log.trace,
debug: log.debug,
log: log.info,
warn: log.warn,
error: log.error,
}
window.addEventListener('unhandledrejection', err =>
log.error(err.reason),
)
window.addEventListener('error', err => log.error(err.error), true)
}
}

View file

@ -32,6 +32,10 @@ export default defineNuxtConfig({
querystring: 'rollup-plugin-node-polyfills/polyfills/qs', querystring: 'rollup-plugin-node-polyfills/polyfills/qs',
}, },
vite: { vite: {
// to make use of `TAURI_PLATFORM`, `TAURI_ARCH`, `TAURI_FAMILY`,
// `TAURI_PLATFORM_VERSION`, `TAURI_PLATFORM_TYPE` and `TAURI_DEBUG`
// env variables
envPrefix: ['VITE_', 'TAURI_'],
define: { define: {
'import.meta.env.__BUILD_TIME__': JSON.stringify(new Date().toISOString()), 'import.meta.env.__BUILD_TIME__': JSON.stringify(new Date().toISOString()),
'import.meta.env.__BUILD_COMMIT__': JSON.stringify(process.env.COMMIT_REF || ''), 'import.meta.env.__BUILD_COMMIT__': JSON.stringify(process.env.COMMIT_REF || ''),
@ -39,7 +43,7 @@ export default defineNuxtConfig({
'process.mock': ((!isCI || isPreview) && process.env.MOCK_USER) || 'false', 'process.mock': ((!isCI || isPreview) && process.env.MOCK_USER) || 'false',
}, },
build: { build: {
target: 'esnext', target: process.env.TAURI_PLATFORM ? ['es2021', 'chrome100', 'safari13'] : 'esnext',
}, },
plugins: [ plugins: [
Inspect(), Inspect(),

View file

@ -18,7 +18,8 @@
"postinstall": "nuxi prepare && simple-git-hooks" "postinstall": "nuxi prepare && simple-git-hooks"
}, },
"dependencies": { "dependencies": {
"@fnando/sparkline": "^0.3.10" "@fnando/sparkline": "^0.3.10",
"tauri-plugin-log-api": "github:tauri-apps/tauri-plugin-log"
}, },
"devDependencies": { "devDependencies": {
"@antfu/eslint-config": "^0.33.1", "@antfu/eslint-config": "^0.33.1",

View file

@ -56,6 +56,7 @@ specifiers:
shiki-es: ^0.1.2 shiki-es: ^0.1.2
simple-git-hooks: ^2.8.1 simple-git-hooks: ^2.8.1
std-env: ^3.3.1 std-env: ^3.3.1
tauri-plugin-log-api: github:tauri-apps/tauri-plugin-log
theme-vitesse: ^0.6.0 theme-vitesse: ^0.6.0
tippy.js: ^6.3.7 tippy.js: ^6.3.7
typescript: ^4.9.3 typescript: ^4.9.3
@ -70,6 +71,7 @@ specifiers:
dependencies: dependencies:
'@fnando/sparkline': 0.3.10 '@fnando/sparkline': 0.3.10
tauri-plugin-log-api: github.com/tauri-apps/tauri-plugin-log/b58475bbc410fa78eb69276c62d0b64c91c07914
devDependencies: devDependencies:
'@antfu/eslint-config': 0.33.1_s5ps7njkmjlaqajutnox5ntcla '@antfu/eslint-config': 0.33.1_s5ps7njkmjlaqajutnox5ntcla
@ -1314,6 +1316,11 @@ packages:
rollup: 2.79.1 rollup: 2.79.1
dev: true dev: true
/@tauri-apps/api/1.2.0:
resolution: {integrity: sha512-lsI54KI6HGf7VImuf/T9pnoejfgkNoXveP14pVV7XarrQ46rOejIVJLFqHI9sRReJMGdh2YuCoI3cc/yCWCsrw==}
engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
dev: false
/@tiptap/core/2.0.0-beta.204: /@tiptap/core/2.0.0-beta.204:
resolution: {integrity: sha512-MH4LQE6rvX+DAy83tZH5E6gaA/hO5A6F/w5ZM6En5PcRhNsgpfQl+kjRfeVQYahxouc1mzetayhRe4XQ8PAwng==} resolution: {integrity: sha512-MH4LQE6rvX+DAy83tZH5E6gaA/hO5A6F/w5ZM6En5PcRhNsgpfQl+kjRfeVQYahxouc1mzetayhRe4XQ8PAwng==}
dependencies: dependencies:
@ -8008,7 +8015,6 @@ packages:
/tslib/2.4.1: /tslib/2.4.1:
resolution: {integrity: sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==} resolution: {integrity: sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==}
dev: true
/tsutils/3.21.0_typescript@4.9.3: /tsutils/3.21.0_typescript@4.9.3:
resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==} resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==}
@ -9020,3 +9026,12 @@ packages:
compress-commons: 4.1.1 compress-commons: 4.1.1
readable-stream: 3.6.0 readable-stream: 3.6.0
dev: true dev: true
github.com/tauri-apps/tauri-plugin-log/b58475bbc410fa78eb69276c62d0b64c91c07914:
resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/b58475bbc410fa78eb69276c62d0b64c91c07914}
name: tauri-plugin-log-api
version: 0.1.0
dependencies:
'@tauri-apps/api': 1.2.0
tslib: 2.4.1
dev: false