add: config about logger

This commit is contained in:
viletyy 2023-01-18 21:44:39 +08:00
parent 2da6c684f6
commit cbd7871d8a
5 changed files with 22 additions and 9 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/pkg/
/spec/reports/
/tmp/
/log/

View File

@ -1,7 +1,7 @@
PATH
remote: .
specs:
gitea-client (0.10.0)
gitea-client (1.1.0)
rest-client (~> 2.1.0)
GEM

View File

@ -13,6 +13,7 @@ module Gitea
# @option opts [string] gitea_token [选填] 用户token
def initialize(opts)
@config = Config.new(opts)
Gitea::Common::Logging.set_log_file(@config.log_filepath)
@http = Http.new(@config)
end

View File

@ -2,7 +2,7 @@ module Gitea
module Api
class Config < Common::Struct::Base
attrs :domain, :base_url, :username, :password, :token, :open_timeout, :read_timeout
attrs :domain, :base_url, :username, :password, :token, :open_timeout, :read_timeout, :log_filepath
def initialize(opts = {})
super(opts)

View File

@ -65,7 +65,10 @@ module Gitea
headers[:params] = http_options[:query] || {}
headers[:params].merge!({access_token: @config.token}) if @config.token
logger.debug("Send HTTP request, verb: #{verb}, http_options: #{http_options}")
logger.info("Gitea Client Begin a Request!...")
logger.info("Send HTTP request, verb: #{verb}, http_options: #{http_options}")
logger.info("Relative Url: #{api_url}")
logger.info("Headers: #{headers}")
request = RestClient::Request.new(
:method => verb,
@ -102,13 +105,21 @@ module Gitea
end
response = RestClient::Response.create(nil, response, request)
response.return!
end
end
begin
if response.headers.has_key?(:x_total)
return {data: JSON.parse(response), total_data: response.headers[:x_total]}
else
return JSON.parse(response)
end
rescue => e
logger.error(e.to_s)
return {}
end
logger.info("Gitea Hat Client Success End a Request!...")
if response.headers.has_key?(:x_total)
return {data: JSON.parse(response), total_data: response.headers[:x_total]}
else
return JSON.parse(response)
end rescue {}
end
def get_user_agent