Current section
Files
Jump to
Current section
Files
lib/text_sanitizer.ex
#
# This file is part of PrettyLog.
#
# Copyright 2019-2021 Ispirata Srl
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
defmodule PrettyLog.TextSanitizer do
def sanitize_keyword(keywords) do
Enum.map(keywords, fn {k, v} ->
{k, sanitize(v)}
end)
end
def sanitize(value) when is_atom(value) do
string_atom = Atom.to_string(value)
with "Elixir." <> no_prefix_string <- string_atom do
no_prefix_string
end
end
def sanitize(value) when is_binary(value) do
value
end
def sanitize(value) when is_integer(value) do
Integer.to_string(value)
end
def sanitize(value) when is_float(value) do
Float.to_string(value)
end
def sanitize(value) when is_pid(value) or is_port(value) or is_reference(value) do
inspect(value)
end
def sanitize([{k, _v} | _tail] = value) when is_atom(k) do
base64_encode_term(value)
end
def sanitize([a | _tail] = value) when is_atom(a) do
base64_encode_term(value)
end
def sanitize(value) when is_list(value) do
:erlang.iolist_to_binary(value)
rescue
_ ->
base64_encode_term(value)
end
def sanitize(value) do
base64_encode_term(value)
end
defp base64_encode_term(value) do
b64_encoded =
value
|> :erlang.term_to_binary()
|> Base.encode64()
"base64-encoded-ext-term:" <> b64_encoded
end
end