diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml new file mode 100644 index 0000000..0d94547 --- /dev/null +++ b/.github/workflows/changelog.yml @@ -0,0 +1,59 @@ +name: Changelog Check + +on: + # Trigger this workflow on push (merge) events, + # but ignore the main branch + push: + branches-ignore: + - main + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + changelog: + runs-on: ubuntu-latest + + steps: + - name: "[Git] Checkout code" + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: "[Setup] Open /usr/local/bin Permissions" + run: sudo chmod -R a+rwx /usr/local/bin + + - name: "[Setup] Cache Dependencies" + id: cache + uses: actions/cache@v2 + with: + path: | + /usr/local/bin/git-cl + key: ${{ runner.os }}-release + restore-keys: | + ${{ runner.os }}-release + + - name: "[Changelog] Install build tools" + uses: fwal/setup-swift@v1 + if: steps.cache.outputs.cache-hit != 'true' + with: + swift-version: "5.2" + + - name: "[Changelog] Install git-ci" + if: steps.cache.outputs.cache-hit != 'true' + run: | + cd /tmp/ + git clone https://github.com/uptech/git-cl.git git-cl + cd ./git-cl + make -j$(nproc) + sudo make install + sudo chmod a+rwx /usr/local/bin/git-cl + cd ${GITHUB_WORKSPACE} + + - name: "[Changelog] Fail if no changelog entries" + run: | + # echo output for debugging + git cl unreleased | tail -n +4 | xargs + # test output and fail step if empty + [ "$(git cl unreleased | tail -n +4 | xargs)" ] || exit 1 + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c95c095 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,166 @@ +name: CI + +on: push + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + include: + - elixir: 1.6 + otp: 20.3 + - elixir: 1.7.x + otp: 21.x + - elixir: 1.8.x + otp: 21.x + - elixir: 1.9.x + otp: 22.x + - elixir: 1.10.x + otp: 23.x + - elixir: 1.11.x + otp: 23.x + - elixir: 1.12.x + otp: 24.x + + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: erlef/setup-elixir@v1 + with: + elixir-version: ${{matrix.elixir}} + otp-version: ${{matrix.otp}} + - name: Cache Dependencies + uses: actions/cache@v2 + with: + path: | + deps + _build/test/lib + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix-${{ hashFiles('mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix- + - name: Install Dependencies + run: mix deps.get + + - name: Build Dependencies + env: + MIX_ENV: test + run: mix deps.compile + + - name: Build Project + env: + MIX_ENV: test + run: mix compile --warnings-as-errors + + lint: + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: erlef/setup-elixir@v1 + with: + elixir-version: '1.11.x' + otp-version: '23.2.1' + - name: Cache Dependencies + uses: actions/cache@v2 + with: + path: | + deps + _build/test/lib + key: ${{ runner.os }}-mix-test-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }} + restore-keys: | + ${{ runner.os }}-mix-test- + - name: Check formatting + env: + MIX_ENV: test + run: mix format --check-formatted + + test: + runs-on: ubuntu-latest + needs: build + + strategy: + matrix: + include: + - elixir: 1.6 + otp: 20.3 + - elixir: 1.7.x + otp: 21.x + - elixir: 1.8.x + otp: 21.x + - elixir: 1.9.x + otp: 22.x + - elixir: 1.10.x + otp: 23.x + - elixir: 1.11.x + otp: 23.x + + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: erlef/setup-elixir@v1 + with: + elixir-version: ${{matrix.elixir}} + otp-version: ${{matrix.otp}} + - name: Cache Dependencies + uses: actions/cache@v2 + with: + path: | + deps + _build/test/lib + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix-${{ hashFiles('mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix- + - name: Run tests + run: mix test + + coverage: + runs-on: ubuntu-latest + needs: test + + strategy: + matrix: + include: + - elixir: 1.6 + otp: 20.3 + - elixir: 1.7.x + otp: 21.x + - elixir: 1.8.x + otp: 21.x + - elixir: 1.9.x + otp: 22.x + - elixir: 1.10.x + otp: 23.x + - elixir: 1.11.x + otp: 23.x + + env: + MIX_ENV: test + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: erlef/setup-elixir@v1 + with: + elixir-version: ${{matrix.elixir}} + otp-version: ${{matrix.otp}} + - name: Cache Dependencies + uses: actions/cache@v2 + with: + path: | + deps + _build/test/lib + key: ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix-${{ hashFiles('mix.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-mix- + - name: Build ExCoveralls + run: mix compile excoveralls + + - name: Coverage Summary + run: mix coveralls + + - name: Publish Coverage + run: mix coveralls.github diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5527884 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,138 @@ +name: Release + +on: + # Trigger this workflow on push (merge) events, + # but only for the main branch + push: + branches: + - main + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSIONS_OTP: '24.0.1' + VERSIONS_ELIXIR: '1.12.x' + HEX_API_KEY: ${{ secrets.HEX_API_KEY }} + +jobs: + release: + name: Create Release + runs-on: ubuntu-latest + + steps: + - name: "[Git] Checkout code" + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: "[Git] Capture message" + run: | + # Actions do not support multiline `::set-output`s + echo 'COMMIT_MSG<> "$GITHUB_ENV" + git log -n1 --pretty='%B' >> "$GITHUB_ENV" + echo 'EOM' >> "$GITHUB_ENV" + + - name: "[Setup] Open /usr/local/bin Permissions" + run: sudo chmod -R a+rwx /usr/local/bin + + - name: "[Setup] Cache Dependencies" + id: cache + uses: actions/cache@v2 + with: + path: | + deps + _build/test/lib + /usr/local/bin/git-cl + key: ${{ runner.os }}-release + restore-keys: | + ${{ runner.os }}-release + + - name: "[Setup] Setup Elixir" + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ env.VERSIONS_ELIXIR }} + otp-version: ${{ env.VERSIONS_OTP }} + + - name: "[Version] Install" + uses: gittools/actions/gitversion/setup@v0.9.6 + with: + versionSpec: '5.x' + + - name: "[Version] Calculate" + id: gitversion # step id used as reference for output values + uses: gittools/actions/gitversion/execute@v0.9.6 + + - name: "[Version] Capture" + run: echo "RELEASE_VERSION=${{ steps.gitversion.outputs.semVer }}" >> $GITHUB_ENV + + - name: "[Version] Update" + run: | + sed -i 's/@version ".*"/@version "${{env.RELEASE_VERSION}}"/gi' mix.exs + + - name: "[Changelog] Install build tools" + uses: fwal/setup-swift@v1 + if: steps.cache.outputs.cache-hit != 'true' + with: + swift-version: "5.2" + + - name: "[Changelog] Install git-ci" + if: steps.cache.outputs.cache-hit != 'true' + run: | + cd /tmp/ + git clone https://github.com/uptech/git-cl.git git-cl + cd ./git-cl + make -j$(nproc) + sudo make install + sudo chmod a+rwx /usr/local/bin/git-cl + cd ${GITHUB_WORKSPACE} + + - name: "[Changelog] Fail if no changelog entries" + run: | + # echo output for debugging + git cl unreleased | tail -n +4 | xargs + # test output and fail step if empty + [ "$(git cl unreleased | tail -n +4 | xargs)" ] || exit 1 + + - name: "[Changelog] Generate" + run: | + git cl full > CHANGELOG.md + TODAY=$(date '+%Y-%m-%d') + sed -i "s/^## \[Unreleased\] - now/## [${{env.RELEASE_VERSION}}] - ${TODAY}/" CHANGELOG.md + sed -i "7,8d" CHANGELOG.md + + - name: "[Release] Commit release updates" + uses: stefanzweifel/git-auto-commit-action@v4 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + # --amend --no-edit doesn't work because of default --message + # (effectively --no-edit is ignored) + commit_message: ${{ env.COMMIT_MSG }} + commit_options: --amend --no-edit + push_options: --force + # so as not to make too shallow for commit --amend and push -f + skip_fetch: true + + - name: "[Release] Create new release" + id: create_release + uses: actions/create-release@v1 + with: + tag_name: ${{env.RELEASE_VERSION}} + release_name: ${{env.RELEASE_VERSION}} + body: | + Please see the CHANGELOG for further details + draft: false + prerelease: false + + - name: "[Release] Setup Elixir for Publishing to Hex.pm" + uses: erlef/setup-elixir@v1 + with: + elixir-version: 1.11.x + otp-version: 23.x + + - name: "[Release] Publish to Hex.pm" + run: | + mix local.hex --force + mix local.rebar --force + mix do deps.get, deps.compile + mix hex.build + mix hex.publish --yes diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 16b5976..0000000 --- a/.travis.yml +++ /dev/null @@ -1,51 +0,0 @@ -language: elixir - -elixir: - - '1.9' -otp_release: - - '22.0.3' - -matrix: - include: - - elixir: '1.3' - otp_release: '18.3' - - elixir: '1.3' - otp_release: '19.0' - - elixir: '1.4' - otp_release: '18.3' - - elixir: '1.4' - otp_release: '19.0' - - elixir: '1.4' - otp_release: '20.0' - - elixir: '1.5' - otp_release: '18.3' - - elixir: '1.5' - otp_release: '19.0' - - elixir: '1.5' - otp_release: '20.0' - - elixir: '1.6' - otp_release: '18.3' - - elixir: '1.6' - otp_release: '19.0' - - elixir: '1.6' - otp_release: '20.0' - - elixir: '1.6' - otp_release: '21.0' - - elixir: '1.7' - otp_release: '20.0' - - elixir: '1.7' - otp_release: '21.0' - - elixir: '1.7' - otp_release: '22.0' - - elixir: '1.8' - otp_release: '20.0' - - elixir: '1.8' - otp_release: '21.0' - - elixir: '1.8' - otp_release: '22.0' - - elixir: '1.9' - otp_release: '20.0' - - elixir: '1.9' - otp_release: '21.0' - - elixir: '1.9' - otp_release: '22.0' diff --git a/CHANGELOG.md b/CHANGELOG.md index 62f56ad..16ccf8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,25 @@ # Changelog -All notable changes to this project will be documented on this page. +All notable changes to this project will be documented in this file. -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). + + +## [0.1.0] - 2021-06-18 -## [0.0.1] - 2019-06-16 ### Added -- Initial project release +- coverage check + +### Changed +- min version now 1.6 +- replace Travis with Github CI workflow +- add Github release workflow +- updated CI badge in README +- moduledoc for main module to pull from README +- MIT licence replaced with Apache 2.0 licence +- min version +- add travis config with matrix tests + +### Fixed +- formatting + diff --git a/LICENSE b/LICENSE index 0937906..63bbe02 100644 --- a/LICENSE +++ b/LICENSE @@ -1,23 +1,13 @@ -MIT License +Copyright 2021 @OldhamMade -Copyright (c) 2019 @OldhamMade +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: + http://www.apache.org/licenses/LICENSE-2.0 -The above copyright notice and this permission notice (including the -next paragraph) shall be included in all copies or substantial -portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/README.md b/README.md index 40ce69c..14ffbb7 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ # LoggerEtsBackend -[![current build status on Travis-CI.org](https://travis-ci.org/OldhamMade/logger_ets_backend.svg?branch=master)][1] +![CI][ci-badge] [![Coverage Status][coverage-badge]][coverage-link] + + A simple `Logger` backend which writes log entries to an ETS table. It does not create or manage the table for you; you must do this external to the logging app. -`LoggerEtsBackend` borrows heavily from [`LoggerFileBackend`][2], and -therefore acts much the same way. +`LoggerEtsBackend` borrows heavily from +[`LoggerFileBackend`][logger_file_backend], and therefore acts much +the same way. ## Rationale @@ -53,6 +56,8 @@ config :logger, :critical_log, **Note:** It is recommended that `metadata_filter` is set for this backend, to ensure only a small subset of log entries are captured. + + ### Examples #### Runtime configuration @@ -115,6 +120,50 @@ config :logger, :ui, metadata_filter: [application: :ui] ``` - -[1]: https://travis-ci.org/OldhamMade/logger_ets_backend -[2]: https://github.com/onkel-dirtus/logger_file_backend +## Contributing + +**Note: the project is made & maintained by a small team of humans, +who on occasion may make mistakes and omissions. Please do not +hesitate to point out if you notice a bug or something missing, and +consider contributing if you can.** + +The project is managed on a best-effort basis, and aims to be "good +enough". If there are features missing please raise a ticket or create +a Pull Request by following these steps: + +1. [Fork it][fork] +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Raise a new pull request via GitHub + +## Liability + +We take no responsibility for the use of our tool, or external +instances provided by third parties. We strongly recommend you abide +by the valid official regulations in your country. Furthermore, we +refuse liability for any inappropriate or malicious use of this +tool. This tool is provided to you in the spirit of free, open +software. + +You may view the LICENSE in which this software is provided to you +[here](./LICENSE). + +> 8. Limitation of Liability. In no event and under no legal theory, +> whether in tort (including negligence), contract, or otherwise, +> unless required by applicable law (such as deliberate and grossly +> negligent acts) or agreed to in writing, shall any Contributor be +> liable to You for damages, including any direct, indirect, special, +> incidental, or consequential damages of any character arising as a +> result of this License or out of the use or inability to use the +> Work (including but not limited to damages for loss of goodwill, +> work stoppage, computer failure or malfunction, or any and all +> other commercial damages or losses), even if such Contributor +> has been advised of the possibility of such damages. + + +[logger_file_backend]: https://github.com/onkel-dirtus/logger_file_backend +[ci-badge]: https://github.com/OldhamMade/logger_ets_backend/workflows/CI/badge.svg +[coverage-badge]: https://coveralls.io/repos/github/OldhamMade/logger_ets_backend/badge.svg?branch=main +[coverage-link]: https://coveralls.io/github/OldhamMade/logger_ets_backend?branch=main +[fork]: https://github.com/OldhamMade/logger_ets_backend/fork diff --git a/lib/logger_ets_backend.ex b/lib/logger_ets_backend.ex index 2402c08..0b57cf5 100644 --- a/lib/logger_ets_backend.ex +++ b/lib/logger_ets_backend.ex @@ -1,5 +1,8 @@ defmodule LoggerEtsBackend do - @moduledoc false + @moduledoc "README.md" + |> File.read!() + |> String.split("") + |> Enum.fetch!(1) @behaviour :gen_event @@ -12,13 +15,14 @@ defmodule LoggerEtsBackend do end def handle_event( - {level, _gl, {Logger, msg, ts, md}}, - %{level: min_level, metadata_filter: metadata_filter} = state - ) do - if (is_nil(min_level) or Logger.compare_levels(level, min_level) != :lt) - and metadata_matches?(md, metadata_filter) do + {level, _gl, {Logger, msg, ts, md}}, + %{level: min_level, metadata_filter: metadata_filter} = state + ) do + if (is_nil(min_level) or Logger.compare_levels(level, min_level) != :lt) and + metadata_matches?(md, metadata_filter) do log_event(level, msg, ts, md, state) end + {:ok, state} end @@ -44,25 +48,33 @@ defmodule LoggerEtsBackend do end def metadata_matches?(_md, nil), do: true - def metadata_matches?(_md, []), do: true # all of the filter keys are present + # all of the filter keys are present + def metadata_matches?(_md, []), do: true + def metadata_matches?(md, [{key, val} | rest]) do case Keyword.fetch(md, key) do {:ok, ^val} -> metadata_matches?(md, rest) - _ -> false #fail on first mismatch + + # fail on first mismatch + _ -> + false end end defp take_metadata(metadata, :all), do: metadata + defp take_metadata(metadata, keys) do - metadatas = Enum.reduce(keys, [], fn key, acc -> - case Keyword.fetch(metadata, key) do - {:ok, val} -> - [{key, val} | acc] - :error -> - acc - end - end) + metadatas = + Enum.reduce(keys, [], fn key, acc -> + case Keyword.fetch(metadata, key) do + {:ok, val} -> + [{key, val} | acc] + + :error -> + acc + end + end) Enum.reverse(metadatas) end @@ -77,6 +89,7 @@ defmodule LoggerEtsBackend do metadata: nil, metadata_filter: nil } + configure(name, opts, state) end @@ -87,15 +100,17 @@ defmodule LoggerEtsBackend do table = Keyword.get(opts, :table) level = Keyword.get(opts, :level) - metadata = Keyword.get(opts, :metadata, []) # + # + metadata = Keyword.get(opts, :metadata, []) metadata_filter = Keyword.get(opts, :metadata_filter) - %{ state | - name: name, - table: table, - level: level, - metadata: metadata, - metadata_filter: metadata_filter + %{ + state + | name: name, + table: table, + level: level, + metadata: metadata, + metadata_filter: metadata_filter } end end diff --git a/mix.exs b/mix.exs index aaf0ce2..4e4bb8b 100644 --- a/mix.exs +++ b/mix.exs @@ -1,18 +1,31 @@ defmodule LoggerEtsBackend.MixProject do use Mix.Project - @version "0.0.2" + @version "0.1.0" @github "https://github.com/OldhamMade/logger_ets_backend" def project do [ app: :logger_ets_backend, version: @version, - elixir: "~> 1.3", + elixir: "~> 1.6", + build_embedded: Mix.env() == :prod, description: description(), package: package(), deps: deps(), - docs: docs() + + # Docs + name: "logger_ets_backend", + docs: docs(), + + # Coverage + test_coverage: [tool: ExCoveralls], + preferred_cli_env: [ + coveralls: :test, + "coveralls.detail": :test, + "coveralls.post": :test, + "coveralls.html": :test + ] ] end @@ -24,6 +37,7 @@ defmodule LoggerEtsBackend.MixProject do defp deps do [ + {:excoveralls, ">= 0.0.0", only: [:dev, :test]}, {:ex_doc, ">= 0.0.0", only: :dev} ] end @@ -34,17 +48,14 @@ defmodule LoggerEtsBackend.MixProject do defp package() do [ - name: "logger_ets_backend", - files: ~w(lib .formatter.exs mix.exs README.md LICENSE CHANGELOG.md), - licenses: ["MIT"], - links: %{"GitHub" => @github} + maintainers: ["Phillip Oldham"], + licenses: ["Apache 2.0"], + links: %{"GitHub" => @github}, + files: ~w(lib .formatter.exs mix.exs README.md LICENSE CHANGELOG.md) ] end defp docs do - [extras: ["README.md"], - main: "readme", - source_ref: "v#{@version}", - source_url: @github] + [extras: ["README.md"], main: "readme", source_ref: "v#{@version}", source_url: @github] end end diff --git a/mix.lock b/mix.lock index 4c499e8..6d0b1a2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,7 +1,18 @@ %{ - "earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"}, - "ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, - "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, - "nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"}, + "certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"}, + "ex_doc": {:hex, :ex_doc, "0.24.2", "e4c26603830c1a2286dae45f4412a4d1980e1e89dc779fcd0181ed1d5a05c8d9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e134e1d9e821b8d9e4244687fb2ace58d479b67b282de5158333b0d57c6fb7da"}, + "excoveralls": {:hex, :excoveralls, "0.14.1", "14140e4ef343f2af2de33d35268c77bc7983d7824cb945e6c2af54235bc2e61f", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "4a588f9f8cf9dc140cc1f3d0ea4d849b2f76d5d8bee66b73c304bb3d3689c8b0"}, + "hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"}, + "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, + "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, + "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"}, + "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, + "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"}, + "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } diff --git a/test/logger_ets_backend_test.exs b/test/logger_ets_backend_test.exs index 81b790d..7414e09 100644 --- a/test/logger_ets_backend_test.exs +++ b/test/logger_ets_backend_test.exs @@ -11,42 +11,44 @@ defmodule LoggerEtsBackendTest do setup_all do :ets.new(@name, [:ordered_set, :public, :named_table]) - config level: :debug + config(level: :debug) end describe "error handling during setup" do test "does not crash if table does not exist" do - config table: nil + config(table: nil) - Logger.debug "foo" + Logger.debug("foo") assert {:error, :already_present} = Logger.add_backend(@backend) end end describe "basic " do setup do - config [ + config( table: @name, level: :debug, metadata: [], metadata_filter: nil - ] + ) - on_exit fn -> + on_exit(fn -> :ets.delete_all_objects(@name) - end + end) end test "add log entry" do Logger.info("simple message") - Logger.flush() # to ensure messages are written to ets + # to ensure messages are written to ets + Logger.flush() assert ets_size() == 1 end test "can log utf8 chars" do Logger.info("ß\uFFaa\u0222") - Logger.flush() # to ensure messages are written to ets + # to ensure messages are written to ets + Logger.flush() {_ts, _level, msg, _md} = log() assert ets_size() == 1 @@ -54,42 +56,51 @@ defmodule LoggerEtsBackendTest do end test "can configure level" do - config level: :info + config(level: :info) Logger.debug("hello") assert ets_size() == 0 end test "can configure metadata_filter to capture" do - config metadata_filter: [capture: true] + config(metadata_filter: [capture: true]) Logger.debug("should be skipped", capture: false) - Logger.flush() # to ensure messages are written to ets + # to ensure messages are written to ets + Logger.flush() assert ets_size() == 0 Logger.debug("should be logged", capture: true) - Logger.flush() # to ensure messages are written to ets + # to ensure messages are written to ets + Logger.flush() assert ets_size() == 1 end test "metadata_matches?" do - assert metadata_matches?([a: 1], [a: 1]) == true # exact match - assert metadata_matches?([b: 1], [a: 1]) == false # total mismatch - assert metadata_matches?([b: 1], nil) == true # default to allow - assert metadata_matches?([b: 1, a: 1], [a: 1]) == true # metadata is superset of filter - assert metadata_matches?([c: 1, b: 1, a: 1], [b: 1, a: 1]) == true # multiple filter keys subset of metadata - assert metadata_matches?([a: 1], [b: 1, a: 1]) == false # multiple filter keys superset of metadata + # exact match + assert metadata_matches?([a: 1], a: 1) == true + # total mismatch + assert metadata_matches?([b: 1], a: 1) == false + # default to allow + assert metadata_matches?([b: 1], nil) == true + # metadata is superset of filter + assert metadata_matches?([b: 1, a: 1], a: 1) == true + # multiple filter keys subset of metadata + assert metadata_matches?([c: 1, b: 1, a: 1], b: 1, a: 1) == true + # multiple filter keys superset of metadata + assert metadata_matches?([a: 1], b: 1, a: 1) == false end test "can configure metadata" do - config metadata: [:user_id, :auth] + config(metadata: [:user_id, :auth]) Logger.metadata(auth: true) Logger.metadata(user_id: 11) Logger.metadata(user_id: 13) Logger.debug("hello") - Logger.flush() # to ensure messages are written to ets + # to ensure messages are written to ets + Logger.flush() {_ts, _level, msg, md} = log() @@ -99,20 +110,20 @@ defmodule LoggerEtsBackendTest do end test "Allow `:all` to metadata" do - config metadata: [] - Logger.debug "metadata", metadata1: "foo", metadata2: "bar" + config(metadata: []) + Logger.debug("metadata", metadata1: "foo", metadata2: "bar") Logger.flush() {_ts, _level, _msg, md} = log() assert md == [] - config metadata: [:metadata3] - Logger.debug "metadata", metadata3: "foo", metadata4: "bar" + config(metadata: [:metadata3]) + Logger.debug("metadata", metadata3: "foo", metadata4: "bar") Logger.flush() {_ts, _level, _msg, md} = log() assert md[:metadata3] == "foo" - config metadata: :all - Logger.debug "metadata", metadata5: "foo", metadata6: "bar" + config(metadata: :all) + Logger.debug("metadata", metadata5: "foo", metadata6: "bar") Logger.flush() {_ts, _level, _msg, md} = log() assert md[:metadata5] == "foo" @@ -122,7 +133,8 @@ defmodule LoggerEtsBackendTest do defp config(opts) do Logger.configure_backend(@backend, opts) - Process.sleep(100) # let the logging process catch-up + # let the logging process catch-up + Process.sleep(100) end defp ets_size() do @@ -131,6 +143,7 @@ defmodule LoggerEtsBackendTest do defp log() do key = :ets.last(@name) + case :ets.lookup(@name, key) do [val] -> val [] -> nil diff --git a/test/test_helper.exs b/test/test_helper.exs index d6eee7d..5281714 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,2 @@ -:application.start :logger +:application.start(:logger) ExUnit.start()