6 from systemd
import journal
8 urllib3
.disable_warnings()
9 http
= urllib3
.PoolManager()
11 webhook_url
= "<%= @slack_logger %>"
12 webhook_username
= "<%= @slack_logger_username %>"
14 # These icons are made by CSSCREME.COM
15 # http://csscreme.com/freeicons/
16 # License: GNU (GENERAL PUBLIC LICENSE)
17 webhook_icon
= "https://git.immae.eu/releases/logger.png"
21 1: "6684672", # dark dark red
22 2: "13369344", # dark red
24 4: "16737792", # orange
27 7: "16777215", # white
30 def send_to_discord(messages
):
31 def send_chunk(chunk
, priority
, fields
=None):
33 "avatar_url": webhook_icon
,
34 "username": webhook_username
,
37 "description": "```\n{}\n```".format(chunk
[0:2000]),
38 "color": priorities
[priority
],
43 if fields
is not None:
44 data
["content"] = "@everyone"
46 for field
in ["CLIENT_IP", "PATH", "METHOD", "STATUS_CODE", "USER_ID", "FLAG", "LATENCY"]:
47 value
= fields
.get(field
)
49 data
["embeds"][-1]["fields"].append({
50 "name": field
.replace("_", " ").capitalize(),
55 encoded
= json
.dumps(data
).encode('utf-8')
57 r
= http
.request("POST", webhook_url
,
58 headers
={'Content-Type': 'application/json'}
,
61 b
= json
.loads(r
.data
)
62 time
.sleep(b
["retry_after"] / 900)
63 r
= http
.request("POST", webhook_url
,
64 headers
={'Content-Type': 'application/json'}
,
67 print("Error when sending message: {}".format(r
.status
))
71 for message
in messages
:
72 m_priority
= int(message
.get("PRIORITY", 5))
76 if len(chunk
) > 0 and m_priority
!= priority
:
77 send_chunk(chunk
, priority
)
81 send_chunk(message
["MESSAGE"], m_priority
, fields
=message
)
85 if "SYSLOG_IDENTIFIER" in message
:
86 line
= "[{}] {}".format(message
["SYSLOG_IDENTIFIER"], message
["MESSAGE"])
88 line
= message
["MESSAGE"]
89 if len(line
) + len(chunk
) > 2000 - 17:
90 send_chunk(chunk
, priority
)
95 send_chunk(chunk
, priority
)
98 j
.add_match(_SYSTEMD_UNIT
="cryptoportfolio-app.service")
100 j
.add_match(UNIT
="cryptoportfolio-app.service")
107 send_to_discord(lines
)