Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
defgenx committed Apr 12, 2024
1 parent 7fe09ab commit af94dfa
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 84 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
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ gleam_stdlib = "~> 0.36"
gleam_erlang = "~> 0.25"

[dev-dependencies]
gleeunit = "~> 0.11"
gleeunit = "~> 1.0"
8 changes: 4 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
packages = [
{ 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 = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
]

[requirements]
gleam_erlang = { version = "~> 0.17" }
gleam_stdlib = { version = "~> 0.24" }
gleeunit = { version = "~> 0.6" }
gleam_erlang = { version = "~> 0.25" }
gleam_stdlib = { version = "~> 0.36" }
gleeunit = { version = "~> 1.0" }
110 changes: 55 additions & 55 deletions src/glog.gleam
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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 glog/level.{
Alert, ConfigLevel, Critical, Debug, Emergency, Info, Level, Notice, Warning,
}
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

/// 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 +23,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,13 +38,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) })
}

Expand All @@ -64,7 +62,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)),
)
}

Expand All @@ -82,7 +80,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)),
)
}

Expand Down Expand Up @@ -157,7 +155,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
Expand All @@ -173,7 +171,7 @@ pub fn add_fields(logger: Glog, f: Fields) -> Glog {
/// |> emergency("er")
/// ```
pub fn emergency(logger: Glog, message: String) -> Glog {
log(logger, Emergency, message)
log(logger, level.Emergency, message)
}

/// Prints Emergency log with current fields stored and the given message template and values
Expand All @@ -189,35 +187,35 @@ pub fn emergency(logger: Glog, message: String) -> Glog {
/// |> emergencyf("~p is the new ~p", [arg.new("foo"), arg.new("bar")])
/// ```
pub fn emergencyf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Emergency, string, values)
logf(logger, level.Emergency, string, values)
}

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

/// Prints Alert 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 alertf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Alert, string, values)
logf(logger, level.Alert, string, values)
}

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

/// Prints Critical 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 criticalf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Critical, string, values)
logf(logger, level.Critical, string, values)
}

/// Prints Err log with current fields stored and the given message
Expand All @@ -238,60 +236,60 @@ pub fn errorf(logger: Glog, string: String, values: Args) -> Glog {
///
/// Calling this function return a new Glog. Old Glog can still be used.
pub fn warning(logger: Glog, message: String) -> Glog {
log(logger, Warning, message)
log(logger, level.Warning, message)
}

/// Prints Warning 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 warningf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Warning, string, values)
logf(logger, level.Warning, string, values)
}

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

/// Prints Info 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 infof(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Info, string, values)
logf(logger, level.Info, string, values)
}

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

/// Prints Notice 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 noticef(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Notice, string, values)
logf(logger, level.Notice, string, values)
}

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

/// Prints Debug 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 debugf(logger: Glog, string: String, values: Args) -> Glog {
logf(logger, Debug, string, values)
logf(logger, level.Debug, string, values)
}

// Private function handling the print logic for any level
fn log(logger: Glog, level: Level, message: String) -> Glog {
fn log(logger: Glog, level: level.Level, message: String) -> Glog {
let new_logger =
logger
|> add("msg", message)
Expand All @@ -301,7 +299,7 @@ fn log(logger: Glog, level: Level, message: String) -> Glog {
}

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

Expand All @@ -311,9 +309,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,15 +320,15 @@ fn fields_to_dynamic(fields: Fields) -> Map(Atom, Dynamic) {
}

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

/// Sets log level for given handler
pub fn set_handler_log_level(handler: String, level: ConfigLevel) {
pub fn set_handler_log_level(handler: String, level: level.ConfigLevel) {
set_handler_config_value(
atom.create_from_string(handler),
atom.create_from_string("level"),
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.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 af94dfa

Please sign in to comment.