diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4530d21..d2348f4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/gleam.toml b/gleam.toml index ed1d7b4..aca448a 100644 --- a/gleam.toml +++ b/gleam.toml @@ -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" \ No newline at end of file diff --git a/manifest.toml b/manifest.toml index e051299..594d751 100644 --- a/manifest.toml +++ b/manifest.toml @@ -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" } diff --git a/src/glog.gleam b/src/glog.gleam index 5435b6d..0a7ab44 100644 --- a/src/glog.gleam +++ b/src/glog.gleam @@ -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 @@ -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 @@ -40,13 +41,13 @@ 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) }) } @@ -54,7 +55,7 @@ pub fn get_field(logger: Glog, key: String) -> Result(Field, Nil) { /// /// ### Usage /// ```gleam -/// import glog.{Glog} +/// import glog.{type Glog} /// /// let logger: Glog = glog.new() /// logger @@ -64,7 +65,7 @@ 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)), ) } @@ -72,7 +73,7 @@ pub fn add(logger: Glog, key: String, value: any) -> Glog { /// /// ### Usage /// ```gleam -/// import glog.{Glog} +/// import glog.{type Glog} /// import glog/field /// /// let logger: Glog = glog.new() @@ -82,7 +83,7 @@ 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)), ) } @@ -90,7 +91,7 @@ pub fn add_field(logger: Glog, f: Field) -> Glog { /// /// ### Usage /// ```gleam -/// import glog.{Glog} +/// import glog.{type Glog} /// import glog/field /// /// let logger: Glog = glog.new() @@ -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() @@ -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() @@ -149,7 +150,7 @@ 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() @@ -157,7 +158,7 @@ pub fn add_error(logger: Glog, error: a) -> Glog { /// |> 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 @@ -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 @@ -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 @@ -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 { @@ -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 @@ -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)) @@ -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), ) } @@ -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 diff --git a/src/glog/arg.gleam b/src/glog/arg.gleam index 2335b73..9c30038 100644 --- a/src/glog/arg.gleam +++ b/src/glog/arg.gleam @@ -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) } diff --git a/src/glog/field.gleam b/src/glog/field.gleam index 62c2fe1..8577fbc 100644 --- a/src/glog/field.gleam +++ b/src/glog/field.gleam @@ -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) } diff --git a/src/glog/level.gleam b/src/glog/level.gleam index 03d970f..383185b 100644 --- a/src/glog/level.gleam +++ b/src/glog/level.gleam @@ -1,32 +1,32 @@ /// system is unusable -pub external type Emergency +pub type Emergency /// action must be taken immediately -pub external type Alert +pub type Alert /// critical conditions -pub external type Critical +pub type Critical /// error conditions -pub external type Error +pub type Error /// warning conditions -pub external type Warning +pub type Warning /// normal but significant conditions -pub external type Notice +pub type Notice /// informational messages -pub external type Info +pub type Info /// debug-level messages -pub external type Debug +pub type Debug /// all levels -pub external type All +pub type All /// No level -pub external type None +pub type None /// Logger levels valid list pub type Level { diff --git a/test/glog/field_test.gleam b/test/glog/field_test.gleam index 04dde4a..a3af8ba 100644 --- a/test/glog/field_test.gleam +++ b/test/glog/field_test.gleam @@ -3,13 +3,13 @@ import glog/field import gleam/dynamic pub fn key_value_test() { - let field = field.new("foo", "bar") + let f = field.new("foo", "bar") - field - |> field.key() + f + |> field.key |> should.equal("foo") - field - |> field.value() + f + |> field.value |> should.equal(dynamic.from("bar")) }