Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cyteon committed Jul 15, 2024
1 parent 4199586 commit 382bdf3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 4 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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" }
39 changes: 39 additions & 0 deletions src/discord_gleam/logging.gleam
Original file line number Diff line number Diff line change
@@ -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,
)
}
14 changes: 8 additions & 6 deletions test/example_bot.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import discord_gleam/http/endpoints
import discord_gleam/logging
import gleam/io

pub fn main(token: String) {
Expand All @@ -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(_) -> {
Expand Down

0 comments on commit 382bdf3

Please sign in to comment.