mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-18 06:07:48 +00:00
feat: 保存 renderer 日志到文件
This commit is contained in:
parent
d3089b8940
commit
4c625b172c
12 changed files with 61 additions and 31 deletions
|
|
@ -2,7 +2,7 @@ import { db, Tables } from './db'
|
||||||
import type { FetchTracksResponse } from '../renderer/api/track'
|
import type { FetchTracksResponse } from '../renderer/api/track'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import * as musicMetadata from 'music-metadata'
|
import * as musicMetadata from 'music-metadata'
|
||||||
import { APIs } from './CacheAPIsName'
|
import { APIs } from './CacheAPIsName'
|
||||||
|
|
@ -203,7 +203,7 @@ class Cache {
|
||||||
if (api === APIs.SongDetail) {
|
if (api === APIs.SongDetail) {
|
||||||
const cache = this.get(api, req.query)
|
const cache = this.get(api, req.query)
|
||||||
if (cache) {
|
if (cache) {
|
||||||
logger.debug(`[cache] Cache hit for ${req.path}`)
|
log.debug(`[cache] Cache hit for ${req.path}`)
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -220,7 +220,7 @@ class Cache {
|
||||||
)
|
)
|
||||||
if (!isAudioFileExists) return
|
if (!isAudioFileExists) return
|
||||||
|
|
||||||
logger.debug(`[cache] Audio cache hit for ${req.path}`)
|
log.debug(`[cache] Audio cache hit for ${req.path}`)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: [
|
data: [
|
||||||
|
|
@ -306,9 +306,9 @@ class Cache {
|
||||||
|
|
||||||
await fs.writeFile(`${path}/${id}-${br}.${type}`, buffer, error => {
|
await fs.writeFile(`${path}/${id}-${br}.${type}`, buffer, error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return logger.error(`[cache] cacheAudio failed: ${error}`)
|
return log.error(`[cache] cacheAudio failed: ${error}`)
|
||||||
}
|
}
|
||||||
logger.info(`Audio file ${id}-${br}.${type} cached!`)
|
log.info(`Audio file ${id}-${br}.${type} cached!`)
|
||||||
|
|
||||||
db.upsert(Tables.Audio, {
|
db.upsert(Tables.Audio, {
|
||||||
id,
|
id,
|
||||||
|
|
@ -318,7 +318,7 @@ class Cache {
|
||||||
updateAt: Date.now(),
|
updateAt: Date.now(),
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.info(`[cache] cacheAudio ${id}-${br}.${type}`)
|
log.info(`[cache] cacheAudio ${id}-${br}.${type}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import path from 'path'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import SQLite3 from 'better-sqlite3'
|
import SQLite3 from 'better-sqlite3'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import { createFileIfNotExist } from './utils'
|
import { createFileIfNotExist } from './utils'
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
const isDev = process.env.NODE_ENV === 'development'
|
||||||
|
|
@ -29,7 +29,7 @@ class DB {
|
||||||
)
|
)
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
logger.info('[db] Initializing database...')
|
log.info('[db] Initializing database...')
|
||||||
|
|
||||||
createFileIfNotExist(this.dbFilePath)
|
createFileIfNotExist(this.dbFilePath)
|
||||||
|
|
||||||
|
|
@ -42,7 +42,7 @@ class DB {
|
||||||
this.sqlite.pragma('auto_vacuum = FULL')
|
this.sqlite.pragma('auto_vacuum = FULL')
|
||||||
this.initTables()
|
this.initTables()
|
||||||
|
|
||||||
logger.info('[db] Database initialized')
|
log.info('[db] Database initialized')
|
||||||
}
|
}
|
||||||
|
|
||||||
initTables() {
|
initTables() {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import {
|
||||||
import Store from 'electron-store'
|
import Store from 'electron-store'
|
||||||
import { release } from 'os'
|
import { release } from 'os'
|
||||||
import path, { join } from 'path'
|
import path, { join } from 'path'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import { initIpcMain } from './ipcMain'
|
import { initIpcMain } from './ipcMain'
|
||||||
|
|
||||||
const isWindows = process.platform === 'win32'
|
const isWindows = process.platform === 'win32'
|
||||||
|
|
@ -18,7 +18,7 @@ const isMac = process.platform === 'darwin'
|
||||||
const isLinux = process.platform === 'linux'
|
const isLinux = process.platform === 'linux'
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
const isDev = process.env.NODE_ENV === 'development'
|
||||||
|
|
||||||
logger.info('[index] Main process start')
|
log.info('[index] Main process start')
|
||||||
|
|
||||||
// Disable GPU Acceleration for Windows 7
|
// Disable GPU Acceleration for Windows 7
|
||||||
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
|
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
|
||||||
|
|
@ -99,7 +99,7 @@ async function createWindow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(async () => {
|
app.whenReady().then(async () => {
|
||||||
logger.info('[index] App ready')
|
log.info('[index] App ready')
|
||||||
createWindow()
|
createWindow()
|
||||||
handleWindowEvents()
|
handleWindowEvents()
|
||||||
initIpcMain(win)
|
initIpcMain(win)
|
||||||
|
|
@ -113,10 +113,10 @@ app.whenReady().then(async () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
} = require('electron-devtools-installer')
|
} = require('electron-devtools-installer')
|
||||||
installExtension(REACT_DEVELOPER_TOOLS.id).catch(err =>
|
installExtension(REACT_DEVELOPER_TOOLS.id).catch(err =>
|
||||||
logger.info('An error occurred: ', err)
|
log.info('An error occurred: ', err)
|
||||||
)
|
)
|
||||||
installExtension(REDUX_DEVTOOLS.id).catch(err =>
|
installExtension(REDUX_DEVTOOLS.id).catch(err =>
|
||||||
logger.info('An error occurred: ', err)
|
log.info('An error occurred: ', err)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { BrowserWindow, ipcMain, app } from 'electron'
|
||||||
import { db, Tables } from './db'
|
import { db, Tables } from './db'
|
||||||
import { IpcChannels } from './IpcChannelsName'
|
import { IpcChannels } from './IpcChannelsName'
|
||||||
import cache from './cache'
|
import cache from './cache'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { APIs } from './CacheAPIsName'
|
import { APIs } from './CacheAPIsName'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,17 @@
|
||||||
* @see https://www.npmjs.com/package/electron-log
|
* @see https://www.npmjs.com/package/electron-log
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import logger from 'electron-log'
|
import log from 'electron-log'
|
||||||
import pc from 'picocolors'
|
import pc from 'picocolors'
|
||||||
|
|
||||||
Object.assign(console, logger.functions)
|
Object.assign(console, log.functions)
|
||||||
logger.transports.console.format = `${pc.dim('{h}:{i}:{s}{scope}')} › {text}`
|
log.variables.process = 'main'
|
||||||
logger.transports.file.level = 'info'
|
log.transports.console.format = `[{process}] ${pc.dim(
|
||||||
|
'{h}:{i}:{s}{scope}'
|
||||||
|
)} {level} › {text}`
|
||||||
|
log.transports.file.level = 'info'
|
||||||
|
|
||||||
logger.info(
|
log.info(
|
||||||
`\n\n██╗ ██╗███████╗███████╗██████╗ ██╗ █████╗ ██╗ ██╗███╗ ███╗██╗ ██╗███████╗██╗ ██████╗
|
`\n\n██╗ ██╗███████╗███████╗██████╗ ██╗ █████╗ ██╗ ██╗███╗ ███╗██╗ ██╗███████╗██╗ ██████╗
|
||||||
╚██╗ ██╔╝██╔════╝██╔════╝██╔══██╗██║ ██╔══██╗╚██╗ ██╔╝████╗ ████║██║ ██║██╔════╝██║██╔════╝
|
╚██╗ ██╔╝██╔════╝██╔════╝██╔══██╗██║ ██╔══██╗╚██╗ ██╔╝████╗ ████║██║ ██║██╔════╝██║██╔════╝
|
||||||
╚████╔╝ █████╗ ███████╗██████╔╝██║ ███████║ ╚████╔╝ ██╔████╔██║██║ ██║███████╗██║██║
|
╚████╔╝ █████╗ ███████╗██████╔╝██║ ███████║ ╚████╔╝ ██╔████╔██║██║ ██║███████╗██║██║
|
||||||
|
|
@ -21,6 +24,6 @@ logger.info(
|
||||||
╚═╝ ╚══════╝╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝\n`
|
╚═╝ ╚══════╝╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝\n`
|
||||||
)
|
)
|
||||||
|
|
||||||
export default logger
|
export default log
|
||||||
|
|
||||||
logger.info(`[logger] logger initialized`)
|
log.info(`[logger] logger initialized`)
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import { createDirIfNotExist } from './utils'
|
import { createDirIfNotExist } from './utils'
|
||||||
|
|
@ -10,4 +10,4 @@ if (isDev) {
|
||||||
createDirIfNotExist(devUserDataPath)
|
createDirIfNotExist(devUserDataPath)
|
||||||
app.setPath('appData', devUserDataPath)
|
app.setPath('appData', devUserDataPath)
|
||||||
}
|
}
|
||||||
logger.info(`[index] userData path: ${app.getPath('userData')}`)
|
log.info(`[index] userData path: ${app.getPath('userData')}`)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
import { IpcChannels } from '@/main/IpcChannelsName'
|
import { IpcChannels } from '@/main/IpcChannelsName'
|
||||||
const { contextBridge, ipcRenderer } = require('electron')
|
const { contextBridge, ipcRenderer } = require('electron')
|
||||||
|
const log = require('electron-log')
|
||||||
|
|
||||||
|
const isDev = process.env.NODE_ENV === 'development'
|
||||||
|
|
||||||
|
log.transports.file.level = 'info'
|
||||||
|
log.variables.process = 'renderer'
|
||||||
|
log.transports.console.format = isDev
|
||||||
|
? `[{process}] {text}`
|
||||||
|
: `[{process}] {h}:{i}:{s}{scope} {level} › {text}`
|
||||||
|
contextBridge.exposeInMainWorld('log', log)
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('ipcRenderer', {
|
contextBridge.exposeInMainWorld('ipcRenderer', {
|
||||||
sendSync: ipcRenderer.sendSync,
|
sendSync: ipcRenderer.sendSync,
|
||||||
|
|
@ -14,6 +24,7 @@ contextBridge.exposeInMainWorld('ipcRenderer', {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('env', {
|
contextBridge.exposeInMainWorld('env', {
|
||||||
isElectron: true,
|
isElectron: true,
|
||||||
isEnableTitlebar:
|
isEnableTitlebar:
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import * as Sentry from '@sentry/node'
|
import * as Sentry from '@sentry/node'
|
||||||
import * as Tracing from '@sentry/tracing'
|
import * as Tracing from '@sentry/tracing'
|
||||||
import pkg from '../../package.json'
|
import pkg from '../../package.json'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
|
|
||||||
logger.info(`[sentry] sentry initializing`)
|
log.info(`[sentry] sentry initializing`)
|
||||||
|
|
||||||
Sentry.init({
|
Sentry.init({
|
||||||
dsn: 'https://2aaaa67f1c3d4d6baefafa5d58fcf340@o436528.ingest.sentry.io/6274637',
|
dsn: 'https://2aaaa67f1c3d4d6baefafa5d58fcf340@o436528.ingest.sentry.io/6274637',
|
||||||
|
|
@ -16,4 +16,4 @@ Sentry.init({
|
||||||
tracesSampleRate: 1.0,
|
tracesSampleRate: 1.0,
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.info(`[sentry] sentry initialized`)
|
log.info(`[sentry] sentry initialized`)
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import { pathCase } from 'change-case'
|
import { pathCase } from 'change-case'
|
||||||
import cookieParser from 'cookie-parser'
|
import cookieParser from 'cookie-parser'
|
||||||
import express, { Request, Response } from 'express'
|
import express, { Request, Response } from 'express'
|
||||||
import logger from './logger'
|
import log from './log'
|
||||||
import cache from './cache'
|
import cache from './cache'
|
||||||
import fileUpload from 'express-fileupload'
|
import fileUpload from 'express-fileupload'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
logger.info('[server] starting http server')
|
log.info('[server] starting http server')
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
const isDev = process.env.NODE_ENV === 'development'
|
||||||
const isProd = process.env.NODE_ENV === 'production'
|
const isProd = process.env.NODE_ENV === 'production'
|
||||||
|
|
@ -24,7 +24,7 @@ Object.entries(neteaseApi).forEach(([name, handler]) => {
|
||||||
name = pathCase(name)
|
name = pathCase(name)
|
||||||
|
|
||||||
const wrappedHandler = async (req: Request, res: Response) => {
|
const wrappedHandler = async (req: Request, res: Response) => {
|
||||||
logger.debug(`[server] Handling request: ${req.path}`)
|
log.debug(`[server] Handling request: ${req.path}`)
|
||||||
|
|
||||||
// Get from cache
|
// Get from cache
|
||||||
const cacheData = await cache.getForExpress(name, req)
|
const cacheData = await cache.getForExpress(name, req)
|
||||||
|
|
@ -93,5 +93,5 @@ const port = Number(
|
||||||
: process.env.ELECTRON_DEV_NETEASE_API_PORT ?? 3000
|
: process.env.ELECTRON_DEV_NETEASE_API_PORT ?? 3000
|
||||||
)
|
)
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
logger.info(`[server] API server listening on port ${port}`)
|
log.info(`[server] API server listening on port ${port}`)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
2
src/renderer/global.d.ts
vendored
2
src/renderer/global.d.ts
vendored
|
|
@ -1,4 +1,5 @@
|
||||||
import { IpcChannels } from '@/main/IpcChannelsName'
|
import { IpcChannels } from '@/main/IpcChannelsName'
|
||||||
|
import { ElectronLog } from 'electron-log'
|
||||||
|
|
||||||
export {}
|
export {}
|
||||||
|
|
||||||
|
|
@ -20,6 +21,7 @@ declare global {
|
||||||
isMac: boolean
|
isMac: boolean
|
||||||
isWin: boolean
|
isWin: boolean
|
||||||
}
|
}
|
||||||
|
log?: ElectronLog
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import './utils/initLog'
|
||||||
import { StrictMode } from 'react'
|
import { StrictMode } from 'react'
|
||||||
import * as ReactDOMClient from 'react-dom/client'
|
import * as ReactDOMClient from 'react-dom/client'
|
||||||
import { BrowserRouter } from 'react-router-dom'
|
import { BrowserRouter } from 'react-router-dom'
|
||||||
|
|
|
||||||
13
src/renderer/utils/initLog.ts
Normal file
13
src/renderer/utils/initLog.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
export {}
|
||||||
|
|
||||||
|
if (window.log) {
|
||||||
|
Object.assign(console, window.log.functions)
|
||||||
|
window.log.info(
|
||||||
|
`\n\n██╗ ██╗███████╗███████╗██████╗ ██╗ █████╗ ██╗ ██╗███╗ ███╗██╗ ██╗███████╗██╗ ██████╗
|
||||||
|
╚██╗ ██╔╝██╔════╝██╔════╝██╔══██╗██║ ██╔══██╗╚██╗ ██╔╝████╗ ████║██║ ██║██╔════╝██║██╔════╝
|
||||||
|
╚████╔╝ █████╗ ███████╗██████╔╝██║ ███████║ ╚████╔╝ ██╔████╔██║██║ ██║███████╗██║██║
|
||||||
|
╚██╔╝ ██╔══╝ ╚════██║██╔═══╝ ██║ ██╔══██║ ╚██╔╝ ██║╚██╔╝██║██║ ██║╚════██║██║██║
|
||||||
|
██║ ███████╗███████║██║ ███████╗██║ ██║ ██║ ██║ ╚═╝ ██║╚██████╔╝███████║██║╚██████╗
|
||||||
|
╚═╝ ╚══════╝╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝\n`
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue