iT邦幫忙

2022 iThome 鐵人賽

DAY 6
0

You are running a system that consists of a few applications producing many logs.

You want to write a small program that will aggregate those logs and give them labels according to their severity level. All applications in your system use the same log codes, but some of the legacy applications don't support all the codes.

1. Return the logging code label

Implement the LogLevel.to_label/2 function. It should take an integer code and a boolean flag telling you if the log comes from a legacy app, and return the label of a log line as an atom. Unknown log codes and codes unsupported in a legacy app should return an unknown label.

2. Send an alert

Somebody has to be notified when unexpected things happen.

Implement the LogLevel.alert_recipient/2 function to determine to whom the alert needs to be sent. The function should take an integer code and a boolean flag telling you if the log comes from a legacy app, and return the name of the recipient as an atom.

If the log label is error or fatal, send the alert to the ops team. If you receive a log with an unknown label from a legacy system, send the alert to the dev1 team, other unknown labels should be sent to the dev2 team. All other log labels can be safely ignored.


https://exercism.org/tracks/elixir/exercises/log-level

defmodule LogLevel do
  def to_label(level, legacy?) do
    cond do
      level === 0 && legacy? -> :unknown
      level === 0 -> :trace
      level === 1 -> :debug
      level === 2 -> :info
      level === 3 -> :warning
      level === 4 -> :error
      level === 5 && legacy? -> :unknown
      level === 5 -> :fatal
      true -> :unknown
    end
  end

  def alert_recipient(level, legacy?) do
    label = to_label(level, legacy?)

    cond do
      label === :unknown && legacy? -> :dev1
      label in [:error, :fatal] -> :ops
      label !== :unknown -> false
      true -> :dev2
    end
  end
end

上一篇
五.Integers
下一篇
七. Lists
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言