     end
   end
 
-  def handle_event(:flush, state) do
-    #IO.puts("Logs Flush")
+  # server impl
+  def init([]) do
+    {:ok, %{ count: 0, queue: [], timer: nil}}
+  end
 
-    {:ok, state}
+  def handle_call({:log, msg}, from, state) do
+    state = %{ state | count: state.count+1, queue: [msg | state.queue] }
+    state = if state.count >= @max_queue_size do
+      {:noreply, _, state} = handle_call(:flush, from, state)
+      state
+    else
+      state
+    end
+
+    if state.timer do
+      :timer.cancel(state.timer)
+    end
+    {:ok, timer} = :timer.apply_after(1000, __MODULE__, :flush, [__MODULE__])
+    state = %{ state | timer: timer }
+
+    {:reply, :ok, state}
   end
-  def handle_event({level, _group_leader, {Logger, message, timestamp, metadata}}, state) do
-    if (not metadata[:application] in state.ignore_applications) or (level != :debug) do
+
+  def handle_call(:flush, _from, state) do
+    if state.timer do
+      :timer.cancel(state.timer)
+    end
+
+    for {message, timestamp, metadata, level} <- state.queue do
       changelog = Model.Line.changelog(%Model.Line{}, %{
         message: to_string(message),
         level: to_string(level),

  end

  def init(_opts) do
    {:ok, %{ ignore_applications: ignore_applications }}
  end

  def to_json_type(v) do

    {:ok, %{ lines: lines, count: count }}
  end

  defp should_log?(metadata, level) do
    cond do
      level == :debug -> false
      metadata[:application] == :ecto -> false
      true -> true
    end
  end

  # implementation of Logger.log

