Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebodin committed Sep 13, 2023
1 parent 77b5e0e commit c224149
Show file tree
Hide file tree
Showing 22 changed files with 1,345 additions and 1 deletion.
24 changes: 24 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Configuration file for JuliaFormatter.jl
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/

indent = 4
margin = 92
always_for_in = true
whitespace_typedefs = true
whitespace_ops_in_indices = false
remove_extra_newlines = true
import_to_using = false
pipe_to_function_call = false
short_to_long_function_def = false
long_to_short_function_def = false
whitespace_in_kwargs = true
annotate_untyped_fields_with_any = true
format_docstrings = false
conditional_to_if = false
normalize_line_endings = "auto"
trailing_comma = true
join_lines_based_on_source = true
indent_submodule = false
separate_kwargs_with_semicolon = true
surround_whereop_typeparameters = true
always_use_return = false
23 changes: 23 additions & 0 deletions .github/workflows/aqua.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: aqua-lint
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@latest
with:
version: '1'
- uses: actions/checkout@v1
- name: Aqua
shell: julia --color=yes {0}
run: |
using Pkg
Pkg.add(PackageSpec(name="Aqua"))
Pkg.develop(PackageSpec(path=pwd()))
using Polyglot, Aqua
Aqua.test_all(Polyglot; unbound_args = false)
67 changes: 67 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI
on:
push:
branches: [master]
pull_request:
types: [opened, synchronize, reopened]
jobs:
# Workflow from https://github.com/jump-dev/JuMP.jl/blob/master/.github/workflows/format_check.yml
formatter:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@latest
with:
version: '1'
- uses: actions/checkout@v1
- name: Format check
shell: julia --color=yes {0}
run: |
using Pkg
# If you update the version, also update the style guide docs.
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
using JuliaFormatter
format("src", verbose=true)
format("test", verbose=true)
out = String(read(Cmd(`git diff`)))
if isempty(out)
exit(0)
end
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
needs: formatter
strategy:
fail-fast: false
matrix:
include:
- version: '1'
os: windows-latest
arch: x64
- version: '1'
os: ubuntu-latest
arch: x64
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
19 changes: 19 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "Polyglot"
uuid = "211639cc-9b11-4cfd-abc6-8f7477829344"
version = "0.1.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[compat]
LoggingExtras = "1"
julia = "1.7"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
187 changes: 186 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,186 @@
# Polyglot.jl
# Polyglot.jl

A minimal and opinionated way to deal with compositional loggers built with LoggingExtras.jl.

The package also helps users to deal with logs in multiple languages.

## Basic usage

Polyglot will log messages to different locations.

```julia
import Polyglot

log_file = "my_application.log"
polyglot_logger = Polyglot.create_polyglot_logger(log_file)

# Only goes to file
Polyglot.debug("debug message")

# Goes to file and console
Polyglot.info("info message")
Polyglot.warn("warn message")
Polyglot.non_fatal_error("error message")

# Goes to console and file and then runs exit(1) if the session is not iterative.
Polyglot.fatal_error("Application cannot continue")
```

## Log in differnt languages

Polyglot stores some constants that help users deal with logs in different languages.

```julia
log_path = "langs.log"
langs_dict = Dict(
1 => Dict(
"en" => "Hello!",
"pt" => "Olá!",
),
2 => Dict(
"en" => "The file @@@ does not exist.",
"pt" => "O arquivo @@@ não existe.",
)
)
Polyglot.set_dict(langs_dict)
Polyglot.set_language("pt")
polyglot_logger = Polyglot.create_polyglot_logger(log_path)
# It will log the portuguese version "Olá!"
Polyglot.info(1)
# It will display the message "O arquivo file.txt não existe"
Polyglot.info(2, "file.txt")
```

One suggestion to store the codes ans messages for multiple languages is to store it on a TOML file. The function `Polyglot.set_dict` accepts the TOML path as input.

Dictionary TOML:
```toml
[1]
"en" = "Hello!"
"pt" = "Olá!"

[2]
"en" = "The file @@@ does not exist."
"pt" = "O arquivo @@@ não existe."
```

Set dictionary from TOML:
```julia
log_path = "langs.log"
toml_dict_path = "example.toml"
Polyglot.set_dict(toml_dict_path)
Polyglot.set_language("pt")
polyglot_logger = Polyglot.create_polyglot_logger(log_path)
# It will log the portuguese version "Olá!"
Polyglot.info(1)
# It will display the message "O arquivo file.txt não existe"
Polyglot.info(2, "file.txt")
```

## Create logger

The arguments that can be passed using `Polyglot.create_polyglot_logger`:
* `log_file_path`: Log file path. This input must be passed
* `min_level_console`: Minimum level shown in console. Default: Logging.Info
* `min_level_file`: Minimum level shown in file. Default: Logging.Debug
* `append_log`: Boolean input to append logs in existing log file (if true) or overwrite/create log file (if false). Default is false
* `brackets_dict`: select the brackets for each LogLevel. As default,
```julia
brackets_dict = Dict(
"Debug Level" => ["[", "]"],
"Debug" => ["[", "]"],
"Info" => ["[", "]"],
"Warn" => ["[", "]"],
"Error" => ["[", "]"],
"Fatal Error" => ["[", "]"],
)
```
* `level_dict`: defined in order to change the tags. As default,
```julia
level_dict = Dict(
"Debug Level" => "Debug Level",
"Debug" => "Debug",
"Info" => "Info",
"Warn" => "Warn",
"Error" => "Error",
"Fatal Error" => "Fatal Error"
)
```
* `color_dict`: one can customize the tag colors displayed in terminal using this dictionary. As default,
```julia
color_dict = Dict(
"Debug Level" => :cyan,
"Debug" => :cyan,
"Info" => :cyan,
"Warn" => :yellow,
"Error" => :red,
"Fatal Error" => :red
)
```
* `background_reverse_dict`: used to customize the background of a tag in terminal. As default,
```julia
background_reverse_dict = Dict(
"Debug Level" => false,
"Debug" => false,
"Info" => false,
"Warn" => false,
"Error" => false,
"Fatal Error" => true
)
```

The next example shows how to print the tags in lowercase letters, with julia default string colors and background.
```julia
level_dict = Dict(
"Debug Level" => "debug level",
"Debug" => "debug",
"Info" => "info",
"Warn" => "warn",
"Error" => "error",
"Fatal Error" => "fatal error"
)
color_dict = Dict(
"Debug Level" => :normal,
"Debug" => :normal,
"Info" => :normal,
"Warn" => :normal,
"Error" => :normal,
"Fatal Error" => :normal
)
background_reverse_dict = Dict(
"Debug Level" => false,
"Debug" => false,
"Info" => false,
"Warn" => false,
"Error" => false,
"Fatal Error" => false
)

log_file = "my_application.log"
Polyglot.create_polyglot_logger(log_file; level_dict, color_dict, background_reverse_dict)
```

The next example shows how to remove the `info` tag
```julia
log_file = "my_application.log"
brackets_dict = Dict(
"Debug Level" => ["[", "]"],
"Debug" => ["[", "]"],
"Info" => ["", ""],
"Warn" => ["[", "]"],
"Error" => ["[", "]"],
"Fatal Error" => ["[", "]"],
)
level_dict = Dict(
"Debug Level" => "Debug Level",
"Debug" => "Debug",
"Info" => "",
"Warn" => "Warn",
"Error" => "Error",
"Fatal Error" => "Fatal Error",
)
Polyglot.create_polyglot_logger(log_file; brackets_dict, level_dict)
Polyglot.info("info msg")
Polyglot.warn("warn msg")
Polyglot.remove_log_file_path_on_logger_creation(log_file)
```
2 changes: 2 additions & 0 deletions formatter/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
5 changes: 5 additions & 0 deletions formatter/format.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off

SET FORMATTER_DIR=%~dp0

julia --project=%FORMATTER_DIR% %FORMATTER_DIR%\format.jl
25 changes: 25 additions & 0 deletions formatter/format.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Pkg
Pkg.instantiate()

using JuliaFormatter

import Pkg
Pkg.instantiate()

using JuliaFormatter

function apply_formatter()
# 3 times to converge formatting is an heuristic
println("Formatting src and test folders")
for _ in 1:2
format(joinpath(dirname(@__DIR__), "src"))
format(joinpath(dirname(@__DIR__), "test"))
end
formatted_src = format(joinpath(dirname(@__DIR__), "src"))
formatted_test = format(joinpath(dirname(@__DIR__), "test"))
println("src folder formatted: $formatted_src")
println("test folder formatted: $formatted_test")
return nothing
end

apply_formatter()
5 changes: 5 additions & 0 deletions formatter/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

FORMATTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

julia --project=$FORMATTER_DIR $FORMATTER_DIR/format.jl
2 changes: 2 additions & 0 deletions revise/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
5 changes: 5 additions & 0 deletions revise/revise.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off

SET BASEPATH=%~dp0

%JULIA_190% --color=yes --project=%BASEPATH% --load=%BASEPATH%\revise.jl
15 changes: 15 additions & 0 deletions revise/revise.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Pkg
Pkg.instantiate()

using Revise

dir_path = dirname(@__DIR__)

Pkg.activate(dir_path)
Pkg.instantiate()

using Polyglot

@info("""
Polyglot
""")
Loading

0 comments on commit c224149

Please sign in to comment.