Is there a way to preventing secrets from being printed to console or logs?

I have a struct that contains a database key and I want to prevent that key from being printed out when a user inspects the struct or even when the creation of that struct prints out the return value in the console. Is it possible to do this with attributes or any other mechanism?

Example:

defmodule DatabaseConfig do
   defstruct database_url: nil, database_key: nil

   def new do
     %{
           database_url: System.get_env("DB_URL"),
           database_key: System.get_env("DB_KEY")
       }
   end
end

 iex> database_config = DatabaseConfig.new()
 %DatabaseConfig{
    database_url: "https://database.url.com",
    database_key: "xs90ak3k20023=="
 }

Is there a way to make it log out something like "%{..., database_key: xxxxxxxx"} instead of the actual value?

Thanks,
Jeramy

1 Like

Yes, please take a look here.

4 Likes

Thank you!