Skip to content

Commit 37275a0

Browse files
committed
release: add a logger for release tasks
GitHub: groongaGH-43 In this PR, we add a logger for release tasks. Our log will be output in `log/error.log`.
1 parent 1af7c79 commit 37275a0

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

ansible/files/home/packages/webhook/lib/deployer/app.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require "json"
1818
require "openssl"
1919
require_relative "error"
20+
require_relative "logger"
2021
require_relative "payload"
2122
require_relative "response"
2223

@@ -94,7 +95,8 @@ def process_payload!(payload)
9495
def deploy(payload)
9596
Thread.new do
9697
# TODO: call rake tasks for sign packages.
97-
# TODO: write down the errors into log files.
98+
rescue => e
99+
Logger.log("error.log", e.message)
98100
end
99101
end
100102
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (C) 2018 Kouhei Sutou <[email protected]>
2+
# Copyright (C) 2024 Takuya Kodama <[email protected]>
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
require "pathname"
18+
19+
module Deployer
20+
class Logger
21+
LOG_DIR = "log".freeze
22+
23+
class << self
24+
def log(base_name, object)
25+
new(base_name).log(object)
26+
end
27+
end
28+
29+
def initialize(base_name)
30+
@log_dir = prepare_log_dir
31+
@base_name = base_name
32+
@log_path = log_dir + base_name
33+
end
34+
35+
def log(object)
36+
begin
37+
File.open(@log_path, "w") do |log|
38+
if object.is_a?(String)
39+
log.puts(object)
40+
else
41+
PP.pp(object, log)
42+
end
43+
end
44+
rescue SystemCallError
45+
end
46+
end
47+
48+
private
49+
50+
def prepare_log_dir
51+
log_dir = Pathname.new(LOG_DIR)
52+
return log_dir if log_dir.directory?
53+
54+
Pathname.mkdir(LOG_DIR)
55+
log_dir
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)