Skip to content

Commit

Permalink
chore(deps): bump Gleam major stable and deps. (#3)
Browse files Browse the repository at this point in the history
* Update gleam to v1.0.0.
* Update dependencies.
  • Loading branch information
defgenx committed Apr 12, 2024
1 parent a474dad commit 9768a49
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 93 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3.0.0
- uses: erlef/setup-beam@v1.13.0
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
with:
otp-version: "25.1"
gleam-version: "0.24.0"
# elixir-version: "1.14.1"
- run: gleam format --check src test
otp-version: "26.0.2"
gleam-version: "1.0.0"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
8 changes: 5 additions & 3 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ licences = ["Apache-2.0"]
description = "A Gleam implementation of Erlang logger"
repository = { type = "github", user = "defgenx", repo = "glog" }

gleam_version = ">= 1.0.0"

[dependencies]
gleam_stdlib = "~> 0.24"
gleam_erlang = "~> 0.17"
gleam_stdlib = "~> 0.36"
gleam_erlang = "~> 0.25"

[dev-dependencies]
gleeunit = "~> 0.6"
gleeunit = "~> 1.0"
12 changes: 6 additions & 6 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.17.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A3BB3D4A6AFC2E34CAB1A4960F0CBBC4AA1A052D5023477D16B848D86E69948A" },
{ name = "gleam_stdlib", version = "0.25.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "AD0F89928E0B919C8F8EDF640484633B28DBF88630A9E6AE504617A3E3E5B9A2" },
{ name = "gleeunit", version = "0.7.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "5F4FBED3E93CDEDB0570D30E9DECB7058C2D327996B78BB2D245C739C7136010" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
]

[requirements]
gleam_erlang = "~> 0.17"
gleam_stdlib = "~> 0.24"
gleeunit = "~> 0.6"
gleam_erlang = { version = "~> 0.25" }
gleam_stdlib = { version = "~> 0.36" }
gleeunit = { version = "~> 1.0" }
114 changes: 57 additions & 57 deletions src/glog.gleam
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import gleam/erlang/atom.{Atom}
import gleam/map.{Map}
import gleam/erlang/atom.{type Atom}
import gleam/dict.{type Dict}
import gleam/result
import gleam/list
import gleam/option.{None, Option, Some}
import gleam/dynamic.{Dynamic}
import glog/arg.{Args}
import glog/field.{Field, Fields}
import gleam/option.{type Option, None, Some}
import gleam/dynamic.{type Dynamic}
import glog/arg.{type Args}
import glog/field.{type Field, type Fields}
import glog/level.{
Alert, ConfigLevel, Critical, Debug, Emergency, Info, Level, Notice, Warning,
type ConfigLevel, type Level, Alert, Critical, Debug, Emergency, Info, Notice,
Warning,
}

/// A Gleam implementation of Erlang logger
/// Glog is the current "state" of the log to print
pub opaque type Glog {
Glog(fields: Map(Atom, Dynamic))
Glog(fields: Dict(Atom, Dynamic))
}

/// Initializes a new Glog representation
Expand All @@ -25,7 +26,7 @@ pub opaque type Glog {
/// let logger: Glog = glog.new()
/// ```
pub fn new() -> Glog {
Glog(fields: map.new())
Glog(fields: dict.new())
}

/// Initializes a new Glog representation with fields
Expand All @@ -40,21 +41,21 @@ pub fn new_with(with: Option(Fields)) -> Glog {

/// Finds if a field exists from its key
pub fn has_field(logger: Glog, key: String) -> Bool {
map.has_key(logger.fields, atom.create_from_string(key))
dict.has_key(logger.fields, atom.create_from_string(key))
}

/// Fetches a field if it exists from its key
pub fn get_field(logger: Glog, key: String) -> Result(Field, Nil) {
logger.fields
|> map.get(atom.create_from_string(key))
|> dict.get(atom.create_from_string(key))
|> result.map(fn(value) { field.new(key, value) })
}

/// Adds a key/value to the current log
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
///
/// let logger: Glog = glog.new()
/// logger
Expand All @@ -64,15 +65,15 @@ pub fn get_field(logger: Glog, key: String) -> Result(Field, Nil) {
pub fn add(logger: Glog, key: String, value: any) -> Glog {
Glog(
logger.fields
|> map.insert(atom.create_from_string(key), dynamic.from(value)),
|> dict.insert(atom.create_from_string(key), dynamic.from(value)),
)
}

/// Adds a Field to the current log
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
/// import glog/field
///
/// let logger: Glog = glog.new()
Expand All @@ -82,15 +83,15 @@ pub fn add(logger: Glog, key: String, value: any) -> Glog {
pub fn add_field(logger: Glog, f: Field) -> Glog {
Glog(
logger.fields
|> map.insert(atom.create_from_string(field.key(f)), field.value(f)),
|> dict.insert(atom.create_from_string(field.key(f)), field.value(f)),
)
}

/// Adds a Result to the current log
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
/// import glog/field
///
/// let logger: Glog = glog.new()
Expand All @@ -113,7 +114,7 @@ pub fn add_result(logger: Glog, key: String, r: Result(a, b)) -> Glog {
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
/// import glog/field
///
/// let logger: Glog = glog.new()
Expand All @@ -133,7 +134,7 @@ pub fn add_option(logger: Glog, key: String, o: Option(a)) -> Glog {
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
/// import glog/field
///
/// let logger: Glog = glog.new()
Expand All @@ -149,15 +150,15 @@ pub fn add_error(logger: Glog, error: a) -> Glog {
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
/// import glog/field
///
/// let logger: Glog = glog.new()
/// logger
/// |> add_fields([field.new("foo", "bar"), field.new("woo", "zoo")])
/// ```
pub fn add_fields(logger: Glog, f: Fields) -> Glog {
Glog(fields: map.merge(logger.fields, fields_to_dynamic(f)))
Glog(fields: dict.merge(logger.fields, fields_to_dynamic(f)))
}

/// Prints Emergency log with current fields stored and the given message
Expand All @@ -166,7 +167,7 @@ pub fn add_fields(logger: Glog, f: Fields) -> Glog {
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
///
/// let logger: Glog = glog.new()
/// logger
Expand All @@ -182,7 +183,7 @@ pub fn emergency(logger: Glog, message: String) -> Glog {
///
/// ### Usage
/// ```gleam
/// import glog.{Glog}
/// import glog.{type Glog}
///
/// let logger: Glog = glog.new()
/// logger
Expand Down Expand Up @@ -220,14 +221,14 @@ pub fn criticalf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Critical, string, values)
}

/// Prints Err log with current fields stored and the given message
/// Prints Error log with current fields stored and the given message
///
/// Calling this function return a new Glog. Old Glog can still be used.
pub fn error(logger: Glog, message: String) -> Glog {
log(logger, level.Error, message)
}

/// Prints Err log with current fields stored and the given message template and values
/// Prints Error log with current fields stored and the given message template and values
///
/// Calling this function return a new Glog. Old Glog can still be used.
pub fn errorf(logger: Glog, string: String, values: Args) -> Glog {
Expand Down Expand Up @@ -291,18 +292,18 @@ pub fn debugf(logger: Glog, string: String, values: Args) -> Glog {
}

// Private function handling the print logic for any level
fn log(logger: Glog, level: Level, message: String) -> Glog {
fn log(logger: Glog, lvl: Level, message: String) -> Glog {
let new_logger =
logger
|> add("msg", message)
log_with_fields(level, new_logger.fields)
log_with_fields(lvl, new_logger.fields)

logger
}

// Private function handling the printf logic for any level
fn logf(logger: Glog, level: Level, string: String, values: Args) -> Glog {
log(logger, level, sprintf(string, args_to_dynamic(values)))
fn logf(logger: Glog, lvl: Level, string: String, values: Args) -> Glog {
log(logger, lvl, sprintf(string, args_to_dynamic(values)))
}

// Transforms Args to a Dynamic list
Expand All @@ -311,9 +312,9 @@ fn args_to_dynamic(args: Args) -> List(Dynamic) {
|> list.map(fn(a) { dynamic.from(arg.value(a)) })
}

// Transforms Fields to a Atom/Dynamic map
fn fields_to_dynamic(fields: Fields) -> Map(Atom, Dynamic) {
map.from_list(
// Transforms Fields to a Atom/Dynamic dict
fn fields_to_dynamic(fields: Fields) -> Dict(Atom, Dynamic) {
dict.from_list(
fields
|> list.map(fn(f) {
#(atom.create_from_string(field.key(f)), field.value(f))
Expand All @@ -322,19 +323,16 @@ fn fields_to_dynamic(fields: Fields) -> Map(Atom, Dynamic) {
}

/// Sets log level for primary handler
pub fn set_primary_log_level(level: ConfigLevel) {
set_primary_config_value(
atom.create_from_string("level"),
dynamic.from(level),
)
pub fn set_primary_log_level(lvl: ConfigLevel) {
set_primary_config_value(atom.create_from_string("lvl"), dynamic.from(lvl))
}

/// Sets log level for given handler
pub fn set_handler_log_level(handler: String, level: ConfigLevel) {
pub fn set_handler_log_level(handler: String, lvl: ConfigLevel) {
set_handler_config_value(
atom.create_from_string(handler),
atom.create_from_string("level"),
dynamic.from(level),
atom.create_from_string("lvl"),
dynamic.from(lvl),
)
}

Expand All @@ -347,28 +345,30 @@ pub fn set_default_config() {
atom.create_from_string("formatter"),
dynamic.from(#(
dynamic.from(atom.create_from_string("logger_formatter")),
dynamic.from(map.from_list([
#(
atom.create_from_string("single_line"),
atom.create_from_string("true"),
),
#(
atom.create_from_string("legacy_header"),
atom.create_from_string("false"),
),
])),
dynamic.from(
dict.from_list([
#(
atom.create_from_string("single_line"),
atom.create_from_string("true"),
),
#(
atom.create_from_string("legacy_header"),
atom.create_from_string("false"),
),
]),
),
)),
)
}

external fn log_with_fields(Level, Map(Atom, Dynamic)) -> Nil =
"logger" "log"
@external(erlang, "logger", "log")
fn log_with_fields(a: Level, b: Dict(Atom, Dynamic)) -> Nil

external fn set_primary_config_value(Atom, Dynamic) -> Nil =
"logger" "set_primary_config"
@external(erlang, "logger", "set_primary_config")
fn set_primary_config_value(a: Atom, b: Dynamic) -> Nil

external fn set_handler_config_value(Atom, Atom, Dynamic) -> Nil =
"logger" "set_handler_config"
@external(erlang, "logger", "set_handler_config")
fn set_handler_config_value(a: Atom, b: Atom, c: Dynamic) -> Nil

external fn sprintf(String, List(Dynamic)) -> String =
"io_lib" "format"
@external(erlang, "io_lib", "format")
fn sprintf(a: String, b: List(Dynamic)) -> String
4 changes: 2 additions & 2 deletions src/glog/arg.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dynamic.{Dynamic}
import gleam/dynamic.{type Dynamic}

/// Arg opaque representation
pub opaque type Arg {
pub type Arg {
Arg(value: Dynamic)
}

Expand Down
6 changes: 3 additions & 3 deletions src/glog/field.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dynamic.{Dynamic}
import gleam/dynamic.{type Dynamic}

/// Field opaque representation
pub opaque type Field {
/// Field representation
pub type Field {
Field(key: String, value: Dynamic)
}

Expand Down
Loading

0 comments on commit 9768a49

Please sign in to comment.