feat: add log level setting

This commit is contained in:
Simon Ding
2024-07-26 14:22:08 +08:00
parent f5f8434832
commit 1dd61ccbca
6 changed files with 52 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ const (
SettingJacketUrl = "jacket_url" SettingJacketUrl = "jacket_url"
SettingJacketApiKey = "jacket_api_key" SettingJacketApiKey = "jacket_api_key"
SettingDownloadDir = "download_dir" SettingDownloadDir = "download_dir"
SettingLogLevel = "log_level"
) )
const ( const (

View File

@@ -57,6 +57,11 @@ func (c *Client) init() {
log.Infof("set default download dir") log.Infof("set default download dir")
c.SetSetting(downloadDir, "/downloads") c.SetSetting(downloadDir, "/downloads")
} }
logLevel := c.GetSetting(SettingLogLevel)
if logLevel == "" {
log.Infof("set default log level")
c.SetSetting(SettingLogLevel, "info")
}
} }
func (c *Client) generateJwtSerectIfNotExist() { func (c *Client) generateJwtSerectIfNotExist() {

View File

@@ -49,6 +49,8 @@ func (s *Server) Serve() error {
s.r.Use(ginzap.Ginzap(log.Logger().Desugar(), time.RFC3339, false)) s.r.Use(ginzap.Ginzap(log.Logger().Desugar(), time.RFC3339, false))
s.r.Use(ginzap.RecoveryWithZap(log.Logger().Desugar(), true)) s.r.Use(ginzap.RecoveryWithZap(log.Logger().Desugar(), true))
log.SetLogLevel(s.db.GetSetting(db.SettingLogLevel)) //restore log level
s.r.POST("/api/login", HttpHandler(s.Login)) s.r.POST("/api/login", HttpHandler(s.Login))
api := s.r.Group("/api/v1") api := s.r.Group("/api/v1")
@@ -62,7 +64,6 @@ func (s *Server) Serve() error {
setting.GET("/general", HttpHandler(s.GetSetting)) setting.GET("/general", HttpHandler(s.GetSetting))
setting.POST("/auth", HttpHandler(s.EnableAuth)) setting.POST("/auth", HttpHandler(s.EnableAuth))
setting.GET("/auth", HttpHandler(s.GetAuthSetting)) setting.GET("/auth", HttpHandler(s.GetAuthSetting))
setting.POST("/loglevel", HttpHandler(s.SetLogLevel))
} }
activity := api.Group("/activity") activity := api.Group("/activity")
{ {

View File

@@ -35,6 +35,10 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
} }
if in.LogLevel != "" { if in.LogLevel != "" {
log.SetLogLevel(in.LogLevel) log.SetLogLevel(in.LogLevel)
if err := s.db.SetSetting(db.SettingLogLevel, in.LogLevel); err != nil {
return nil, errors.Wrap(err, "save log level")
}
} }
return nil, nil return nil, nil
} }
@@ -42,10 +46,11 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
func (s *Server) GetSetting(c *gin.Context) (interface{}, error) { func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
tmdb := s.db.GetSetting(db.SettingTmdbApiKey) tmdb := s.db.GetSetting(db.SettingTmdbApiKey)
downloadDir := s.db.GetSetting(db.SettingDownloadDir) downloadDir := s.db.GetSetting(db.SettingDownloadDir)
logLevel := s.db.GetSetting(db.SettingLogLevel)
return &GeneralSettings{ return &GeneralSettings{
TmdbApiKey: tmdb, TmdbApiKey: tmdb,
DownloadDir: downloadDir, DownloadDir: downloadDir,
LogLevel: logLevel,
}, nil }, nil
} }
@@ -146,16 +151,3 @@ func (s *Server) DeleteDownloadCLient(c *gin.Context) (interface{}, error) {
s.db.DeleteDownloadCLient(id) s.db.DeleteDownloadCLient(id)
return "success", nil return "success", nil
} }
type logLovelIn struct {
Level string `json:"level"`
}
func (s *Server) SetLogLevel(c *gin.Context) (interface{}, error) {
var in logLovelIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind json")
}
log.SetLogLevel(in.Level)
return "success", nil
}

View File

@@ -49,18 +49,22 @@ class EditSettingData extends AutoDisposeAsyncNotifier<GeneralSetting> {
class GeneralSetting { class GeneralSetting {
String? tmdbApiKey; String? tmdbApiKey;
String? downloadDIr; String? downloadDIr;
String? logLevel;
GeneralSetting({this.tmdbApiKey, this.downloadDIr}); GeneralSetting({this.tmdbApiKey, this.downloadDIr, this.logLevel});
factory GeneralSetting.fromJson(Map<String, dynamic> json) { factory GeneralSetting.fromJson(Map<String, dynamic> json) {
return GeneralSetting( return GeneralSetting(
tmdbApiKey: json["tmdb_api_key"], downloadDIr: json["download_dir"]); tmdbApiKey: json["tmdb_api_key"],
downloadDIr: json["download_dir"],
logLevel: json["log_level"]);
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{}; final Map<String, dynamic> data = <String, dynamic>{};
data['tmdb_api_key'] = tmdbApiKey; data['tmdb_api_key'] = tmdbApiKey;
data['download_dir'] = downloadDIr; data['download_dir'] = downloadDIr;
data["log_level"] = logLevel;
return data; return data;
} }
} }
@@ -257,8 +261,8 @@ class StorageSettingData extends AutoDisposeAsyncNotifier<List<Storage>> {
} }
class Storage { class Storage {
Storage( Storage({
{this.id, this.id,
this.name, this.name,
this.implementation, this.implementation,
this.settings, this.settings,

View File

@@ -37,9 +37,11 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
autovalidateMode: AutovalidateMode.onUserInteraction, autovalidateMode: AutovalidateMode.onUserInteraction,
initialValue: { initialValue: {
"tmdb_api": v.tmdbApiKey, "tmdb_api": v.tmdbApiKey,
"download_dir": v.downloadDIr "download_dir": v.downloadDIr,
"log_level": v.logLevel
}, },
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
FormBuilderTextField( FormBuilderTextField(
name: "tmdb_api", name: "tmdb_api",
@@ -57,6 +59,26 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
// //
validator: FormBuilderValidators.required(), validator: FormBuilderValidators.required(),
), ),
SizedBox(
width: 300,
child: FormBuilderDropdown(
name: "log_level",
decoration: const InputDecoration(
labelText: "日志级别",
icon: Icon(Icons.file_present_rounded),
),
items: const [
DropdownMenuItem(
value: "debug", child: Text("Debug")),
DropdownMenuItem(value: "info", child: Text("Info")),
DropdownMenuItem(
value: "warn", child: Text("Warnning")),
DropdownMenuItem(
value: "error", child: Text("Error")),
],
validator: FormBuilderValidators.required(),
),
),
Center( Center(
child: Padding( child: Padding(
padding: const EdgeInsets.only(top: 28.0), padding: const EdgeInsets.only(top: 28.0),
@@ -72,7 +94,8 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
.read(settingProvider.notifier) .read(settingProvider.notifier)
.updateSettings(GeneralSetting( .updateSettings(GeneralSetting(
tmdbApiKey: values["tmdb_api"], tmdbApiKey: values["tmdb_api"],
downloadDIr: values["download_dir"])); downloadDIr: values["download_dir"],
logLevel: values["log_level"]));
f.then((v) { f.then((v) {
Utils.showSnakeBar("更新成功"); Utils.showSnakeBar("更新成功");
}).onError((e, s) { }).onError((e, s) {