From 382bdf3211f349654c7a726479f48ca8703a3072 Mon Sep 17 00:00:00 2001 From: cyteon Date: Mon, 15 Jul 2024 14:42:23 +0200 Subject: [PATCH] add logging --- README.md | 4 ++-- gleam.toml | 1 + manifest.toml | 5 ++++- src/discord_gleam/logging.gleam | 39 +++++++++++++++++++++++++++++++++ test/example_bot.gleam | 14 +++++++----- 5 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/discord_gleam/logging.gleam diff --git a/README.md b/README.md index c1f6a7b..e14a2e6 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/discord_gleam/) ```sh -gleam add discord_gleam@1 +gleam add discord_gleam ``` ```gleam import discord_gleam pub fn main() { - // TODO: An example of the project in use + todo } ``` diff --git a/gleam.toml b/gleam.toml index a3bfafe..63939af 100644 --- a/gleam.toml +++ b/gleam.toml @@ -20,6 +20,7 @@ gleam_http = ">= 3.6.0 and < 4.0.0" gleam_json = ">= 2.0.0 and < 3.0.0" gleam_hackney = ">= 1.2.0 and < 2.0.0" gleam_otp = ">= 0.10.0 and < 1.0.0" +birl = ">= 1.7.1 and < 2.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml index e5fed99..77dd339 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,6 +2,7 @@ # You typically do not need to edit this file packages = [ + { name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, { name = "certifi", version = "2.12.0", build_tools = ["rebar3"], requirements = [], otp_app = "certifi", source = "hex", outer_checksum = "EE68D85DF22E554040CDB4BE100F33873AC6051387BAF6A8F6CE82272340FF1C" }, { name = "dotenv", version = "3.1.0", build_tools = ["mix"], requirements = [], otp_app = "dotenv", source = "hex", outer_checksum = "01BED84D21BEDD8739AEBAD16489A3CE12D19C2D59AF87377DA65EBB361980D3" }, { name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, @@ -16,16 +17,18 @@ packages = [ { name = "metrics", version = "1.0.1", build_tools = ["rebar3"], requirements = [], otp_app = "metrics", source = "hex", outer_checksum = "69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16" }, { name = "mimerl", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "mimerl", source = "hex", outer_checksum = "A1E15A50D1887217DE95F0B9B0793E32853F7C258A5CD227650889B38839FE9D" }, { name = "parse_trans", version = "3.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "parse_trans", source = "hex", outer_checksum = "620A406CE75DADA827B82E453C19CF06776BE266F5A67CFF34E1EF2CBB60E49A" }, + { name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" }, { name = "ssl_verify_fun", version = "1.1.7", build_tools = ["mix", "rebar3", "make"], requirements = [], otp_app = "ssl_verify_fun", source = "hex", outer_checksum = "FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8" }, { name = "unicode_util_compat", version = "0.7.0", build_tools = ["rebar3"], requirements = [], otp_app = "unicode_util_compat", source = "hex", outer_checksum = "25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521" }, ] [requirements] +birl = { version = ">= 1.7.1 and < 2.0.0"} dotenv = { version = ">= 3.1.0 and < 4.0.0" } gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" } gleam_hackney = { version = ">= 1.2.0 and < 2.0.0" } gleam_http = { version = ">= 3.6.0 and < 4.0.0" } gleam_json = { version = ">= 2.0.0 and < 3.0.0" } -gleam_otp = { version = ">= 0.10.0 and < 1.0.0"} +gleam_otp = { version = ">= 0.10.0 and < 1.0.0" } gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/src/discord_gleam/logging.gleam b/src/discord_gleam/logging.gleam new file mode 100644 index 0000000..0df42d7 --- /dev/null +++ b/src/discord_gleam/logging.gleam @@ -0,0 +1,39 @@ +import birl +import gleam/io +import gleam/list +import gleam/string + +fn get_date_string() -> String { + let now = birl.now() + let date_string_with_timezone = birl.to_date_string(now) + let time_string_with_timezone = birl.to_time_string(now) + + // Removing timezone from date string + let date_parts = string.split(date_string_with_timezone, "+") + let date_without_timezone = case list.first(date_parts) { + Ok(value) -> value + Error(_) -> date_string_with_timezone + } + + // Removing timezone from time string + let time_parts = string.split(time_string_with_timezone, "+") + let time_without_timezone = case list.first(time_parts) { + Ok(value) -> value + Error(_) -> date_string_with_timezone + } + + date_without_timezone <> " " <> time_without_timezone +} + +pub fn println(content: String, log_type: String) { + let log_splitter = case log_type { + "info" -> "\u{001b}[34mINFO" + "warn" -> "\u{001b}[33mWARN" + "error" -> "\u{001b}[31mERROR" + _ -> "\u{001b}[32mLOG" + } + + io.println( + get_date_string() <> " | " <> log_splitter <> "\u{001b}[0m | " <> content, + ) +} diff --git a/test/example_bot.gleam b/test/example_bot.gleam index 5d27d75..e7804cd 100644 --- a/test/example_bot.gleam +++ b/test/example_bot.gleam @@ -1,4 +1,5 @@ import discord_gleam/http/endpoints +import discord_gleam/logging import gleam/io pub fn main(token: String) { @@ -8,13 +9,14 @@ pub fn main(token: String) { let response = endpoints.me(token) case response { Ok(me) -> { - io.println( + logging.println( "Hello from " - <> me.username - <> "#" - <> me.discriminator - <> " with the ID " - <> me.id, + <> me.username + <> "#" + <> me.discriminator + <> " with the ID " + <> me.id, + "", ) } Error(_) -> {