1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
|
defmodule LoggerTest do
use Logger.Case
require Logger
@moduletag formatter: [metadata: [:application, :module]]
setup tags do
:logger.update_handler_config(:default, :formatter, Logger.default_formatter(tags.formatter))
on_exit(fn ->
:logger.update_handler_config(:default, :formatter, Logger.default_formatter())
end)
end
defp msg_with_meta(text) do
msg("module=LoggerTest #{text}")
end
test "levels/0" do
assert [_ | _] = Logger.levels()
assert :info in Logger.levels()
end
test "level/0" do
assert Logger.level() == :debug
Logger.configure(level: :all)
assert Logger.level() == :all
Logger.configure(level: :info)
assert Logger.level() == :info
Logger.configure(level: :notice)
assert Logger.level() == :notice
Logger.configure(level: :warning)
assert Logger.level() == :warning
Logger.configure(level: :error)
assert Logger.level() == :error
Logger.configure(level: :critical)
assert Logger.level() == :critical
Logger.configure(level: :alert)
assert Logger.level() == :alert
Logger.configure(level: :emergency)
assert Logger.level() == :emergency
Logger.configure(level: :none)
assert Logger.level() == :none
after
Logger.configure(level: :debug)
end
test "per-module levels" do
defmodule PerModuleLevels do
def debug, do: Logger.debug("debug_msg")
def error, do: Logger.error("error_msg")
end
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
assert capture_log(fn -> assert PerModuleLevels.error() == :ok end) =~ "error_msg"
Logger.put_module_level(PerModuleLevels, :error)
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) == ""
assert capture_log(fn -> Logger.debug("outer_debug_msg") end) =~ "outer_debug_msg"
assert capture_log(fn -> assert PerModuleLevels.error() == :ok end) =~ "error_msg"
Logger.put_module_level(PerModuleLevels, :debug)
assert capture_log(:error, fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
assert capture_log(:error, fn -> Logger.debug("outer_debug_msg") end) == ""
assert capture_log(:error, fn -> assert PerModuleLevels.error() == :ok end) =~ "error_msg"
Logger.delete_module_level(PerModuleLevels)
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
assert capture_log(fn -> assert PerModuleLevels.error() == :ok end) =~ "error_msg"
Logger.put_module_level(PerModuleLevels, :error)
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) == ""
Logger.delete_all_module_levels()
assert capture_log(fn -> assert PerModuleLevels.debug() == :ok end) =~ "debug_msg"
end
test "per-modules levels" do
modules =
for n <- 1..2 do
name = Module.concat([__MODULE__, PerModulesLevels, to_string(n)])
defmodule name do
def debug, do: Logger.debug("debug_msg")
def error, do: Logger.error("error_msg")
end
name
end
for module <- modules do
assert capture_log(fn -> assert module.debug() == :ok end) =~ "debug_msg"
assert capture_log(fn -> assert module.error() == :ok end) =~ "error_msg"
end
Logger.put_module_level(modules, :error)
for module <- modules do
assert capture_log(fn -> assert module.debug() == :ok end) =~ ""
assert capture_log(fn -> Logger.debug("outer_debug_msg") end) =~ "outer_debug_msg"
assert capture_log(fn -> assert module.error() == :ok end) =~ "error_msg"
end
Logger.put_module_level(modules, :debug)
for module <- modules do
assert capture_log(:error, fn -> assert module.debug() == :ok end) =~ "debug_msg"
assert capture_log(:error, fn -> Logger.debug("outer_debug_msg") end) == ""
assert capture_log(fn -> assert module.error() == :ok end) =~ "error_msg"
end
Logger.delete_module_level(modules)
for module <- modules do
assert capture_log(fn -> assert module.debug() == :ok end) =~ "debug_msg"
assert capture_log(fn -> assert module.error() == :ok end) =~ "error_msg"
end
Logger.put_module_level(modules, :error)
for module <- modules do
assert capture_log(fn -> assert module.debug() == :ok end) == ""
end
Logger.delete_all_module_levels()
for module <- modules do
assert capture_log(fn -> assert module.debug() == :ok end) =~ "debug_msg"
end
end
test "per-application levels" do
Application.load(:eex)
assert Logger.get_module_level(EEx) == []
Logger.put_application_level(:eex, :notice)
assert Logger.get_module_level(EEx) == [{EEx, :notice}]
Logger.put_module_level(EEx, :alert)
assert Logger.get_module_level(EEx) == [{EEx, :alert}]
after
Logger.delete_all_module_levels()
end
test "per-process levels" do
assert Logger.get_process_level(self()) == nil
assert capture_log(fn -> Logger.debug("debug_msg") end) =~ "debug_msg"
Logger.put_process_level(self(), :debug)
assert Logger.get_process_level(self()) == :debug
assert capture_log(fn -> Logger.debug("debug_msg") end) =~ "debug_msg"
Logger.put_process_level(self(), :error)
assert capture_log(fn -> Logger.debug("debug_msg") end) == ""
assert capture_log(fn -> Logger.error("error_msg") end) =~ "error_msg"
Logger.put_process_level(self(), :none)
assert Logger.get_process_level(self()) == :none
assert capture_log(fn -> Logger.debug("debug_msg") end) == ""
assert capture_log(fn -> Logger.error("error_msg") end) == ""
Logger.delete_process_level(self())
assert Logger.get_process_level(self()) == nil
assert capture_log(fn -> Logger.debug("debug_msg") end) =~ "debug_msg"
assert capture_log(fn -> Logger.error("error_msg") end) =~ "error_msg"
end
test "process metadata" do
assert Logger.metadata(data: true) == :ok
assert Logger.metadata() == [data: true]
assert Logger.metadata(data: true) == :ok
assert Logger.metadata() == [data: true]
assert Logger.metadata(meta: 1) == :ok
metadata = Logger.metadata()
assert Enum.sort(metadata) == [data: true, meta: 1]
assert Logger.metadata(data: nil) == :ok
assert Logger.metadata() == [meta: 1]
assert Logger.reset_metadata(meta: 2) == :ok
assert Logger.metadata() == [meta: 2]
assert Logger.reset_metadata(data: true, app: nil) == :ok
assert Logger.metadata() == [data: true]
assert Logger.reset_metadata() == :ok
assert Logger.metadata() == []
end
test "metadata merge" do
assert Logger.metadata(module: Sample) == :ok
assert capture_log(fn ->
assert Logger.bare_log(:info, "ok", application: nil, module: LoggerTest) == :ok
end) =~ msg("module=LoggerTest [info] ok")
end
test "metadata compile-time merge" do
assert Logger.metadata(mfa: {Sample, :foo, 1}) == :ok
assert capture_log(fn ->
assert Logger.bare_log(:info, "ok", application: nil, mfa: {CustomTest, :bar, 2}) ==
:ok
end) =~ msg("module=CustomTest [info] ok")
end
test "metadata merge when the argument function returns metadata" do
assert Logger.metadata(module: Sample) == :ok
fun = fn -> {"ok", [module: "Function"]} end
assert capture_log(fn ->
assert Logger.bare_log(:info, fun, application: nil, module: LoggerTest) == :ok
end) =~ msg("module=Function [info] ok")
end
describe "log with function" do
test "supports iolist" do
fun = fn -> ["ok", ?:, ~c"example"] end
assert capture_log(fn ->
assert Logger.bare_log(:info, fun, application: nil, module: FunctionTest) == :ok
end) =~ msg("module=FunctionTest [info] ok:example")
end
test "supports binaries" do
fun = fn -> "ok:example" end
assert capture_log(fn ->
assert Logger.bare_log(:info, fun, application: nil, module: FunctionTest) == :ok
end) =~ msg("module=FunctionTest [info] ok:example")
end
end
describe "report logging" do
test "supports maps" do
assert capture_log(fn ->
assert Logger.bare_log(:info, %{foo: 10}, application: nil, module: FunctionTest) ==
:ok
end) =~ msg("module=FunctionTest [info] [foo: 10]")
end
test "supports keyword" do
assert capture_log(fn ->
assert Logger.bare_log(:info, foo: 10) == :ok
end) =~ msg("[info] [foo: 10]")
end
test "supports custom report_cb" do
report_cb = fn %{foo: foo} -> {"Foo is ~B", [foo]} end
assert capture_log(fn ->
assert Logger.bare_log(:info, %{foo: 10}, report_cb: report_cb) == :ok
end) =~ msg("[info] Foo is 10")
report_cb = fn %{foo: foo}, _opts -> "Foo is #{foo}" end
assert capture_log(fn ->
assert Logger.bare_log(:info, %{foo: 20}, report_cb: report_cb) == :ok
end) =~ msg("[info] Foo is 20")
end
test "supports translator_inspect_opts for reports" do
Application.put_env(:logger, :translator_inspect_opts, printable_limit: 1)
assert capture_log(fn -> :logger.error(%{foo: "bar"}) end) =~
~S([error] [foo: "b" <> ...])
after
Application.put_env(:logger, :translator_inspect_opts, [])
end
test "supports function that return report" do
assert capture_log(fn ->
assert Logger.bare_log(:info, fn -> %{foo: 10} end) == :ok
end) =~ msg("[info] [foo: 10]")
assert capture_log(fn ->
assert Logger.bare_log(:info, fn -> {%{foo: 10}, []} end) == :ok
end) =~ msg("[info] [foo: 10]")
assert capture_log(fn ->
assert Logger.bare_log(:info, fn -> {[foo: 10], []} end) == :ok
end) =~ msg("[info] [foo: 10]")
end
end
test "compare_levels/2" do
assert Logger.compare_levels(:debug, :debug) == :eq
assert Logger.compare_levels(:debug, :info) == :lt
assert Logger.compare_levels(:debug, :notice) == :lt
assert Logger.compare_levels(:debug, :warning) == :lt
assert Logger.compare_levels(:debug, :error) == :lt
assert Logger.compare_levels(:debug, :critical) == :lt
assert Logger.compare_levels(:debug, :alert) == :lt
assert Logger.compare_levels(:debug, :emergency) == :lt
assert Logger.compare_levels(:info, :debug) == :gt
assert Logger.compare_levels(:info, :info) == :eq
assert Logger.compare_levels(:info, :warning) == :lt
assert Logger.compare_levels(:info, :error) == :lt
assert Logger.compare_levels(:warning, :debug) == :gt
assert Logger.compare_levels(:warning, :info) == :gt
assert Logger.compare_levels(:warning, :warning) == :eq
assert Logger.compare_levels(:warning, :error) == :lt
assert Logger.compare_levels(:error, :debug) == :gt
assert Logger.compare_levels(:error, :info) == :gt
assert Logger.compare_levels(:error, :warning) == :gt
assert Logger.compare_levels(:error, :error) == :eq
end
test "deprecated :warn" do
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert capture_log(fn ->
Logger.log(:warn, "hello") == :ok
end) =~ "[warning]"
assert capture_log(fn ->
Logger.bare_log(:warn, "hello") == :ok
end) =~ "[warning]"
end)
end
describe "levels" do
test "debug/2" do
assert capture_log(fn ->
assert Logger.debug("hello", []) == :ok
end) =~ msg_with_meta("[debug] hello")
assert capture_log(:info, fn ->
assert Logger.debug("hello", []) == :ok
end) == ""
assert capture_log(:info, fn ->
assert Logger.debug(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "info/2" do
assert capture_log(fn ->
assert Logger.info("hello", []) == :ok
end) =~ msg_with_meta("[info] hello")
assert capture_log(:notice, fn ->
assert Logger.info("hello", []) == :ok
end) == ""
assert capture_log(:notice, fn ->
assert Logger.info(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "warning/2" do
assert capture_log(fn ->
assert Logger.warning("hello", []) == :ok
end) =~ msg_with_meta("[warning] hello")
assert capture_log(:error, fn ->
assert Logger.warning("hello", []) == :ok
end) == ""
assert capture_log(:error, fn ->
assert Logger.warning(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "error/2" do
assert capture_log(fn ->
assert Logger.error("hello", []) == :ok
end) =~ msg_with_meta("[error] hello")
assert capture_log(:critical, fn ->
assert Logger.error("hello", []) == :ok
end) == ""
assert capture_log(:critical, fn ->
assert Logger.error(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "critical/2" do
assert capture_log(fn ->
assert Logger.critical("hello", []) == :ok
end) =~ msg_with_meta("[critical] hello")
assert capture_log(:alert, fn ->
assert Logger.critical("hello", []) == :ok
end) == ""
assert capture_log(:alert, fn ->
assert Logger.critical(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "alert/2" do
assert capture_log(fn ->
assert Logger.alert("hello", []) == :ok
end) =~ msg_with_meta("[alert] hello")
assert capture_log(:emergency, fn ->
assert Logger.alert("hello", []) == :ok
end) == ""
assert capture_log(:emergency, fn ->
assert Logger.alert(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
test "emergency/2" do
assert capture_log(fn ->
assert Logger.emergency("hello", []) == :ok
end) =~ msg_with_meta("[emergency] hello")
assert capture_log(:none, fn ->
assert Logger.emergency("hello", []) == :ok
end) == ""
assert capture_log(:none, fn ->
assert Logger.emergency(send(self(), :something), []) == :ok
end) == ""
refute_received :something
end
end
test "remove unused calls at compile time based on matching metadata" do
Logger.configure(
compile_time_application: :sample_app,
compile_time_purge_matching: [
[module: LoggerTest.PurgeMatching, function: "two_filters/0"],
[function: "one_filter/0"],
[custom: true],
[function: "level_filter/0", level_lower_than: :warning],
[application: :sample_app, level_lower_than: :info]
]
)
defmodule PurgeMatching do
def two_filters do
Logger.debug("two_filters")
end
def one_filter do
Logger.debug("one_filter")
end
def custom_filters do
Logger.debug("custom_filters", custom: true)
end
def level_filter do
Logger.info("info_filter")
Logger.warning("warning_filter")
end
def works do
Logger.info("works")
end
def log(level, metadata \\ []) do
Logger.log(level, "ok", metadata)
end
end
assert capture_log(fn -> assert PurgeMatching.works() == :ok end) =~ "works"
assert capture_log(fn -> assert PurgeMatching.one_filter() == :ok end) == ""
assert capture_log(fn -> assert PurgeMatching.two_filters() == :ok end) == ""
assert capture_log(fn -> assert PurgeMatching.custom_filters() == :ok end) == ""
assert capture_log(fn -> assert PurgeMatching.level_filter() == :ok end) =~ "warning_filter"
refute capture_log(fn -> assert PurgeMatching.level_filter() == :ok end) =~ "info_filter"
capture_log(fn -> assert PurgeMatching.log(:info) == :ok end)
capture_log(fn -> assert PurgeMatching.log(:debug) == :ok end)
after
Logger.configure(compile_time_application: nil)
Logger.configure(compile_time_purge_matching: [])
end
test "set application metadata at compile time" do
Logger.configure(compile_time_application: nil)
defmodule SampleNoApp do
def info do
Logger.info("hello")
end
end
assert capture_log(fn ->
assert SampleNoApp.info() == :ok
end) =~ msg("module=LoggerTest.SampleNoApp [info] hello")
Logger.configure(compile_time_application: :sample_app)
defmodule SampleApp do
def info do
Logger.info("hello")
end
end
assert capture_log(fn ->
assert SampleApp.info() == :ok
end) =~ msg("application=sample_app module=LoggerTest.SampleApp [info] hello")
after
Logger.configure(compile_time_application: nil)
end
@tag formatter: [truncate: 4]
test "log/2 truncates messages" do
assert capture_log(fn -> Logger.log(:debug, "hello") end) =~ "hell (truncated)"
end
test "log/2 handles bad Unicode chars" do
output =
capture_log(fn ->
assert Logger.log(:debug, "he" <> <<185>> <> "lo") == :ok
Process.sleep(100)
end)
assert output =~ "FORMATTER ERROR: bad return value"
assert output =~ "** (RuntimeError) bad return value from Logger formatter Logger.Formatter"
end
test "stops the application silently" do
Logger.App.stop()
Application.start(:logger)
end
test "starts the application with warning level" do
Logger.App.stop()
assert %{level: :notice} = :logger.get_primary_config()
Application.put_env(:logger, :level, :warning)
Application.start(:logger)
assert %{level: :warning} = :logger.get_primary_config()
after
Application.delete_env(:logger, :level)
Logger.App.stop()
Application.start(:logger)
end
test "starts the application without a handler" do
Application.put_env(:logger, :default_handler, false)
Logger.App.stop()
Application.start(:logger)
assert :logger.get_handler_config() == []
after
Application.delete_env(:logger, :default_handler)
Logger.App.stop()
Application.start(:logger)
end
test "starts the application with custom formatter" do
Application.put_env(:logger, :default_formatter, format: "yes--$message--yes")
Logger.App.stop()
Application.start(:logger)
assert capture_log(fn -> Logger.log(:debug, "hello") end) =~ "yes--hello--yes"
after
Application.delete_env(:logger, :default_formatter)
Logger.App.stop()
Application.start(:logger)
end
test "starts the application with custom handler" do
Application.put_env(:logger, :default_handler,
level: :error,
formatter: Logger.Formatter.new(format: "no--$message--no")
)
Logger.App.stop()
Application.start(:logger)
assert capture_log(fn -> Logger.log(:debug, "hello") end) == ""
assert capture_log(fn -> Logger.log(:error, "hello") end) =~ "no--hello--no"
after
Application.delete_env(:logger, :default_handler)
Logger.App.stop()
Application.start(:logger)
end
test "starts the application with global metadata" do
Application.put_env(:logger, :metadata, global_meta: :yes)
Logger.App.stop()
Application.start(:logger)
assert :logger.get_primary_config()[:metadata] == %{global_meta: :yes}
after
Application.put_env(:logger, :metadata, [])
Logger.App.stop()
Application.start(:logger)
end
test "writes to stderr on bad default handler config" do
Application.put_env(:logger, :default_handler, config: %{file: 123})
Logger.App.stop()
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
Application.start(:logger)
end) =~
"Could not attach default Logger handler: {:handler_not_added, {:callback_crashed,"
after
Application.delete_env(:logger, :default_handler)
Logger.App.stop()
Application.start(:logger)
end
test "writes to stderr on bad default handler module" do
Application.put_env(:logger, :default_handler, module: :who_knows)
Logger.App.stop()
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
Application.start(:logger)
end) =~
"Could not attach default Logger handler: {:invalid_handler, {:function_not_exported"
after
Application.delete_env(:logger, :default_handler)
Logger.App.stop()
Application.start(:logger)
end
test "configure/1 sets options" do
Logger.configure(sync_threshold: 10)
Logger.configure(truncate: 4048)
Logger.configure(utc_log: true)
Logger.configure(discard_threshold: 10_000)
Logger.configure(translator_inspect_opts: [limit: 3])
assert Application.get_env(:logger, :sync_threshold) == 10
assert Application.get_env(:logger, :utc_log) == true
assert Application.get_env(:logger, :truncate) == 4048
assert Application.get_env(:logger, :discard_threshold) == 10_000
assert Application.get_env(:logger, :translator_inspect_opts) == [limit: 3]
assert {:ok, %{config: log_data}} = :logger.get_handler_config(Logger)
assert log_data.utc_log == true
assert log_data.truncate == 4048
after
Logger.configure(sync_threshold: 20)
Logger.configure(truncate: 8096)
Logger.configure(utc_log: false)
Logger.configure(discard_threshold: 500)
Logger.configure(translator_inspect_opts: [])
end
test "always evaluate messages" do
Logger.configure(
always_evaluate_messages: true,
compile_time_purge_matching: [[level_lower_than: :info]],
level: :error
)
defmodule AlwaysEvaluate do
def compile_purged do
Logger.debug(send(self(), "compile purged"))
end
def runtime_purged do
Logger.info(send(self(), "runtime purged"))
end
def not_purged do
Logger.error(send(self(), "not purged"))
end
end
assert capture_log(fn -> AlwaysEvaluate.not_purged() end) =~ "not purged"
assert_received "not purged"
assert capture_log(fn ->
Logger.configure(level: :error)
AlwaysEvaluate.runtime_purged()
end) == ""
assert_received "runtime purged"
assert capture_log(fn ->
Logger.configure(level: :debug)
AlwaysEvaluate.runtime_purged()
end) =~ "runtime purged"
assert_received "runtime purged"
assert capture_log(fn ->
Logger.configure(level: :error)
AlwaysEvaluate.compile_purged()
end) == ""
assert_received "compile purged"
after
Logger.configure(
level: :debug,
always_evaluate_messages: false,
compile_time_purge_matching: []
)
end
describe "colors" do
@tag formatter: [format: "$message", colors: [enabled: true]]
test "default" do
assert capture_log(fn -> Logger.debug("hello", ansi_color: :yellow) end) ==
IO.ANSI.yellow() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.debug("hello") end) ==
IO.ANSI.cyan() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.info("hello") end) ==
IO.ANSI.normal() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.warning("hello") end) ==
IO.ANSI.yellow() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.error("hello") end) ==
IO.ANSI.red() <> "hello" <> IO.ANSI.reset()
end
@tag formatter: [
format: "$message",
colors: [enabled: true, debug: :magenta, info: :cyan, warning: :magenta, error: :cyan]
]
test "custom" do
assert capture_log(fn -> Logger.debug("hello") end) ==
IO.ANSI.magenta() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.info("hello") end) ==
IO.ANSI.cyan() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.warning("hello") end) ==
IO.ANSI.magenta() <> "hello" <> IO.ANSI.reset()
assert capture_log(fn -> Logger.error("hello") end) ==
IO.ANSI.cyan() <> "hello" <> IO.ANSI.reset()
end
end
describe "OTP integration" do
test "changes level in both" do
assert :logger.get_primary_config().level == :debug
Logger.configure(level: :error)
assert :logger.get_primary_config().level == :error
after
Logger.configure(level: :debug)
end
test "supports module level" do
:logger.set_module_level(__MODULE__, :none)
assert capture_log(fn -> Logger.info("hello") end) == ""
:logger.set_module_level(__MODULE__, :all)
assert capture_log(fn -> Logger.info("hello") end) =~ "hello"
after
:logger.unset_module_level(__MODULE__)
end
test "maps Erlang levels" do
:logger.set_primary_config(:level, :notice)
assert capture_log(fn -> Logger.info("hello") end) =~ "hello"
:logger.set_primary_config(:level, :notice)
assert Logger.level() == :notice
:logger.set_primary_config(:level, :emergency)
assert Logger.level() == :emergency
after
Logger.configure(level: :debug)
end
test "metadata is synchronised" do
Logger.metadata(foo: "bar")
assert Map.new(Logger.metadata()) == :logger.get_process_metadata()
:logger.set_process_metadata(%{bar: "foo"})
assert Map.new(Logger.metadata()) == :logger.get_process_metadata()
end
end
end
|