diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d415080b..69f4137b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#4888](https://github.com/blockscout/blockscout/pull/4888) - Fix fetch_top_tokens method: add nulls last for token holders desc order ### Chore +- [#4998](https://github.com/blockscout/blockscout/pull/4998) - API endpoints logger ## 4.0.0-beta diff --git a/apps/block_scout_web/config/dev.exs b/apps/block_scout_web/config/dev.exs index b841d8efe1..82f9fc6629 100644 --- a/apps/block_scout_web/config/dev.exs +++ b/apps/block_scout_web/config/dev.exs @@ -78,6 +78,11 @@ config :logger, :block_scout_web, level: :debug, path: Path.absname("logs/dev/block_scout_web.log") +config :logger, :api, + level: :debug, + path: Path.absname("logs/dev/api.log"), + metadata_filter: [fetcher: :api] + # Set a higher stacktrace during development. Avoid configuring such # in production as building large stacktraces may be expensive. config :phoenix, :stacktrace_depth, 20 diff --git a/apps/block_scout_web/config/prod.exs b/apps/block_scout_web/config/prod.exs index 4d97721ce4..2882ea50f6 100644 --- a/apps/block_scout_web/config/prod.exs +++ b/apps/block_scout_web/config/prod.exs @@ -33,3 +33,9 @@ config :logger, :block_scout_web, level: :info, path: Path.absname("logs/prod/block_scout_web.log"), rotate: %{max_bytes: 52_428_800, keep: 19} + +config :logger, :api, + level: :debug, + path: Path.absname("logs/prod/api.log"), + metadata_filter: [fetcher: :api], + rotate: %{max_bytes: 52_428_800, keep: 19} diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex new file mode 100644 index 0000000000..65fff404d7 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/api_logger.ex @@ -0,0 +1,19 @@ +defmodule APILogger do + @moduledoc """ + Logger of API ednpoins usage + """ + require Logger + + def log(conn) do + endpoint = + if conn.query_string do + "#{conn.request_path}?#{conn.query_string}" + else + conn.request_path + end + + Logger.debug(endpoint, + fetcher: :api + ) + end +end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex index b6380fc975..9f9de75e8a 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex @@ -14,6 +14,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do """ require Logger + require APILogger import Plug.Conn import Phoenix.Controller, only: [put_view: 2] @@ -29,6 +30,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do {:ok, action} <- translate_action(action), true <- action_accessed?(action, write_actions), {:ok, conn} <- call_controller(conn, controller, action) do + APILogger.log(conn) conn else {:error, :no_action} -> diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex index 6b03afe82a..b04aed7405 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/decompiled_smart_contract_controller.ex @@ -1,10 +1,14 @@ defmodule BlockScoutWeb.API.V1.DecompiledSmartContractController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain alias Explorer.Chain.Hash.Address def create(conn, params) do + APILogger.log(conn) + if auth_token(conn) == actual_token() do with {:ok, hash} <- validate_address_hash(params["address_hash"]), :ok <- Chain.check_address_exists(hash), diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex index 1e65fa75ba..46eff9f5d4 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/health_controller.ex @@ -1,9 +1,13 @@ defmodule BlockScoutWeb.API.V1.HealthController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain def health(conn, _) do + APILogger.log(conn) + with {:ok, number, timestamp} <- Chain.last_db_block_status(), {:ok, cache_number, cache_timestamp} <- Chain.last_cache_block_status() do send_resp(conn, :ok, result(number, timestamp, cache_number, cache_timestamp)) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex index 6b3cf3edd6..14ef262d7e 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/supply_controller.ex @@ -1,9 +1,11 @@ defmodule BlockScoutWeb.API.V1.SupplyController do use BlockScoutWeb, :controller + require APILogger alias Explorer.Chain def supply(conn, _) do + APILogger.log(conn) total_supply = Chain.total_supply() circulating_supply = Chain.circulating_supply() diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex index 59431742ad..f2852b192b 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v1/verified_smart_contract_controller.ex @@ -1,11 +1,15 @@ defmodule BlockScoutWeb.API.V1.VerifiedSmartContractController do use BlockScoutWeb, :controller + require APILogger + alias Explorer.Chain alias Explorer.Chain.Hash.Address alias Explorer.SmartContract.Solidity.Publisher def create(conn, params) do + APILogger.log(conn) + with {:ok, hash} <- validate_address_hash(params["address_hash"]), :ok <- Chain.check_address_exists(hash), {:contract, :not_found} <- {:contract, Chain.check_verified_smart_contract_exists(hash)} do diff --git a/apps/explorer/mix.exs b/apps/explorer/mix.exs index bb6834499d..31512e3624 100644 --- a/apps/explorer/mix.exs +++ b/apps/explorer/mix.exs @@ -104,7 +104,7 @@ defmodule Explorer.Mixfile do # `:spandex` tracing of `:ecto` {:spandex_ecto, "~> 0.6.2"}, # Attach `:prometheus_ecto` to `:ecto` - {:telemetry, "~> 0.4.1"}, + {:telemetry, "~> 0.4.3"}, # `Timex.Duration` for `Explorer.Counters.AverageBlockTime.average_block_time/0` {:timex, "~> 3.7.1"}, {:con_cache, "~> 1.0"}, diff --git a/config/config.exs b/config/config.exs index 6a4dcb75ad..939702dade 100644 --- a/config/config.exs +++ b/config/config.exs @@ -31,7 +31,8 @@ config :logger, {LoggerFileBackend, :token_instances}, {LoggerFileBackend, :reading_token_functions}, {LoggerFileBackend, :pending_transactions_to_refetch}, - {LoggerFileBackend, :empty_blocks_to_refetch} + {LoggerFileBackend, :empty_blocks_to_refetch}, + {LoggerFileBackend, :api} ] config :logger, :console, diff --git a/mix.exs b/mix.exs index c5629a2fed..174eaffeff 100644 --- a/mix.exs +++ b/mix.exs @@ -11,7 +11,7 @@ defmodule BlockScout.Mixfile do apps_path: "apps", deps: deps(), dialyzer: dialyzer(), - elixir: "~> 1.10", + elixir: "~> 1.12", preferred_cli_env: [ credo: :test, dialyzer: :test