├── .expeditor ├── config.yml ├── run_linux_tests.sh ├── run_windows_tests.ps1 ├── update_version.sh └── verify.pipeline.yml ├── .gemtest ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── BUG_TEMPLATE.md │ ├── DESIGN_PROPOSAL.md │ ├── ENHANCEMENT_REQUEST_TEMPLATE.md │ └── SUPPORT_QUESTION.md └── dependabot.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Gemfile ├── LICENSE ├── NOTICE ├── README.md ├── Rakefile ├── VERSION ├── features ├── log.feature ├── steps │ └── log.rb └── support │ ├── env.rb │ └── logit.rb ├── lib └── mixlib │ ├── log.rb │ └── log │ ├── child.rb │ ├── formatter.rb │ ├── logger.rb │ ├── logging.rb │ └── version.rb ├── mixlib-log.gemspec └── spec ├── mixlib ├── log │ ├── child_spec.rb │ ├── formatter_spec.rb │ └── logger_spec.rb └── log_spec.rb └── spec_helper.rb /.expeditor/config.yml: -------------------------------------------------------------------------------- 1 | # Documentation available at https://expeditor.chef.io/docs/getting-started/ 2 | --- 3 | 4 | # Slack channel in Chef Software slack to send notifications about build failures, etc 5 | slack: 6 | notify_channel: chef-found-notify 7 | 8 | # This publish is triggered by the `built_in:publish_rubygems` artifact_action. 9 | rubygems: 10 | - mixlib-log 11 | 12 | github: 13 | # This deletes the GitHub PR branch after successfully merged into the release branch 14 | delete_branch_on_merge: true 15 | # The tag format to use (e.g. v1.0.0) 16 | version_tag_format: "v{{version}}" 17 | # allow bumping the minor release via label 18 | minor_bump_labels: 19 | - "Expeditor: Bump Version Minor" 20 | # allow bumping the major release via label 21 | major_bump_labels: 22 | - "Expeditor: Bump Version Major" 23 | 24 | changelog: 25 | rollup_header: Changes not yet released to rubygems.org 26 | 27 | subscriptions: 28 | # These actions are taken, in order they are specified, anytime a Pull Request is merged. 29 | - workload: pull_request_merged:{{github_repo}}:{{release_branch}}:* 30 | actions: 31 | - built_in:bump_version: 32 | ignore_labels: 33 | - "Expeditor: Skip Version Bump" 34 | - "Expeditor: Skip All" 35 | - bash:.expeditor/update_version.sh: 36 | only_if: built_in:bump_version 37 | - built_in:update_changelog: 38 | ignore_labels: 39 | - "Expeditor: Skip Changelog" 40 | - "Expeditor: Skip All" 41 | - built_in:build_gem: 42 | only_if: built_in:bump_version 43 | - workload: project_promoted:{{agent_id}}:* 44 | actions: 45 | - built_in:rollover_changelog 46 | - built_in:publish_rubygems 47 | 48 | pipelines: 49 | - verify: 50 | description: Pull Request validation tests 51 | public: true 52 | -------------------------------------------------------------------------------- /.expeditor/run_linux_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # This script runs a passed in command, but first setups up the bundler caching on the repo 4 | 5 | set -ue 6 | 7 | export USER="root" 8 | export LANG=C.UTF-8 LANGUAGE=C.UTF-8 9 | 10 | echo "--- bundle install" 11 | bundle config --local path vendor/bundle 12 | bundle install --jobs=7 --retry=3 13 | 14 | echo "+++ bundle exec task" 15 | bundle exec $@ 16 | -------------------------------------------------------------------------------- /.expeditor/run_windows_tests.ps1: -------------------------------------------------------------------------------- 1 | # Stop script execution when a non-terminating error occurs 2 | $ErrorActionPreference = "Stop" 3 | # This will run ruby test on windows platform 4 | 5 | Write-Output "--- Bundle install" 6 | 7 | bundle config --local path vendor/bundle 8 | If ($lastexitcode -ne 0) { Exit $lastexitcode } 9 | 10 | bundle install --jobs=7 --retry=3 11 | If ($lastexitcode -ne 0) { Exit $lastexitcode } 12 | 13 | Write-Output "--- Bundle Execute" 14 | 15 | bundle exec rake 16 | If ($lastexitcode -ne 0) { Exit $lastexitcode } 17 | -------------------------------------------------------------------------------- /.expeditor/update_version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # After a PR merge, Chef Expeditor will bump the PATCH version in the VERSION file. 4 | # It then executes this file to update any other files/components with that new version. 5 | # 6 | 7 | set -evx 8 | 9 | sed -i -r "s/^(\s*)VERSION = \".+\"/\1VERSION = \"$(cat VERSION)\"/" lib/mixlib/log/version.rb 10 | 11 | # Once Expeditor finshes executing this script, it will commit the changes and push 12 | # the commit as a new tag corresponding to the value in the VERSION file. 13 | -------------------------------------------------------------------------------- /.expeditor/verify.pipeline.yml: -------------------------------------------------------------------------------- 1 | --- 2 | expeditor: 3 | cached_folders: 4 | - vendor 5 | defaults: 6 | buildkite: 7 | timeout_in_minutes: 60 8 | 9 | steps: 10 | 11 | - label: run-lint-and-specs-ruby-3.1 12 | command: 13 | - .expeditor/run_linux_tests.sh rake 14 | expeditor: 15 | executor: 16 | docker: 17 | image: ruby:3.1 18 | 19 | - label: run-lint-and-specs-ruby-3.4 20 | command: 21 | - .expeditor/run_linux_tests.sh rake 22 | expeditor: 23 | executor: 24 | docker: 25 | image: ruby:3.4 26 | 27 | - label: run-specs-ruby-3.1-windows 28 | command: 29 | - .expeditor/run_windows_tests.ps1 30 | expeditor: 31 | executor: 32 | docker: 33 | host_os: windows 34 | shell: ["powershell", "-Command"] 35 | image: rubydistros/windows-2019:3.1 36 | user: 'NT AUTHORITY\SYSTEM' 37 | 38 | - label: run-specs-ruby-3.4-windows 39 | command: 40 | - .expeditor/run_windows_tests.ps1 41 | expeditor: 42 | executor: 43 | docker: 44 | host_os: windows 45 | shell: ["powershell", "-Command"] 46 | image: rubydistros/windows-2019:3.4 47 | user: 'NT AUTHORITY\SYSTEM' 48 | -------------------------------------------------------------------------------- /.gemtest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chef/mixlib-log/d9654d66d537f539d38a4ff18718cc53050aa425/.gemtest -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Order is important. The last matching pattern has the most precedence. 2 | 3 | * @chef/chef-infra-reviewers @chef/chef-infra-approvers @chef/chef-infra-owners @jaymzh 4 | .expeditor/ @chef/infra-packages 5 | *.md @chef/docs-team 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: � Bug Report 3 | about: If something isn't working as expected �. 4 | labels: "Status: Untriaged, Type: Bug" 5 | --- 6 | 7 | # Version: 8 | 9 | [Version of the project installed] 10 | 11 | # Environment: 12 | 13 | [Details about the environment such as the Operating System, cookbook details, etc...] 14 | 15 | # Scenario: 16 | 17 | [What you are trying to achieve and you can't?] 18 | 19 | # Steps to Reproduce: 20 | 21 | [If you are filing an issue what are the things we need to do in order to repro your problem?] 22 | 23 | # Expected Result: 24 | 25 | [What are you expecting to happen as the consequence of above reproduction steps?] 26 | 27 | # Actual Result: 28 | 29 | [What actually happens after the reproduction steps?] 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/DESIGN_PROPOSAL.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Design Proposal 3 | about: I have a significant change I would like to propose and discuss before starting 4 | labels: "Status: Untriaged, Type: Design Proposal" 5 | --- 6 | 7 | ### When a Change Needs a Design Proposal 8 | 9 | A design proposal should be opened any time a change meets one of the following qualifications: 10 | 11 | - Significantly changes the user experience of a project in a way that impacts users. 12 | - Significantly changes the underlying architecture of the project in a way that impacts other developers. 13 | - Changes the development or testing process of the project such as a change of CI systems or test frameworks. 14 | 15 | ### Why We Use This Process 16 | 17 | - Allows all interested parties (including any community member) to discuss large impact changes to a project. 18 | - Serves as a durable paper trail for discussions regarding project architecture. 19 | - Forces design discussions to occur before PRs are created. 20 | - Reduces PR refactoring and rejected PRs. 21 | 22 | --- 23 | 24 | 25 | 26 | ## Motivation 27 | 28 | 33 | 34 | ## Specification 35 | 36 | 37 | 38 | ## Downstream Impact 39 | 40 | 41 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/ENHANCEMENT_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🚀 Enhancement Request 3 | about: I have a suggestion (and may want to implement it 🙂)! 4 | labels: "Status: Untriaged" 5 | --- 6 | 7 | ### Describe the Enhancement: 8 | 9 | 10 | ### Describe the Need: 11 | 12 | 13 | ### Current Alternative 14 | 15 | 16 | ### Can We Help You Implement This?: 17 | 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/SUPPORT_QUESTION.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🤗 Support Question 3 | about: If you have a question 💬, please check out our Slack! 4 | --- 5 | 6 | We use GitHub issues to track bugs and feature requests. If you need help please post to our Mailing List or join the Chef Community Slack. 7 | 8 | * Chef Community Slack at https://community-slack.chef.io/. 9 | * Chef Mailing List https://discourse.chef.io/ 10 | 11 | Support issues opened here will be closed and redirected to Slack or Discourse. 12 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "06:00" 8 | timezone: America/Los_Angeles 9 | open-pull-requests-limit: 10 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw? 2 | .DS_Store 3 | coverage 4 | rdoc 5 | pkg 6 | .rake_tasks~ 7 | .bundle 8 | Gemfile.lock 9 | vendor 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --colour 2 | --format documentation 3 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | Style/PreferredHashMethods: 2 | Exclude: 3 | - 'lib/mixlib/log/formatter.rb' 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | 4 | ## [v3.2.5](https://github.com/chef/mixlib-log/tree/v3.2.5) (2025-05-12) 5 | 6 | #### Merged Pull Requests 7 | - add myself to codeowners [#85](https://github.com/chef/mixlib-log/pull/85) ([jaymzh](https://github.com/jaymzh)) 8 | 9 | 10 | 11 | ### Changes not yet released to rubygems.org 12 | 13 | #### Merged Pull Requests 14 | - add myself to codeowners [#85](https://github.com/chef/mixlib-log/pull/85) ([jaymzh](https://github.com/jaymzh)) 15 | - ruby 3.4 upgrade [#86](https://github.com/chef/mixlib-log/pull/86) ([rishichawda](https://github.com/rishichawda)) 16 | 17 | 18 | 19 | ## [v3.1.2](https://github.com/chef/mixlib-log/tree/v3.1.2) (2025-04-10) 20 | 21 | 22 | ## [v3.1.2](https://github.com/chef/mixlib-log/tree/v3.1.2) (2025-04-10) 23 | 24 | ## [v3.2.3](https://github.com/chef/mixlib-log/tree/v3.2.3) (2025-04-01) 25 | 26 | #### Merged Pull Requests 27 | - Chefstyle to Cookstyle migration [#79](https://github.com/chef/mixlib-log/pull/79) ([dafyddcrosby](https://github.com/dafyddcrosby)) 28 | - updating ffi for Ohai [#83](https://github.com/chef/mixlib-log/pull/83) ([johnmccrae](https://github.com/johnmccrae)) 29 | - Relax ffi dependency to allow 1.17.1 and up [#82](https://github.com/chef/mixlib-log/pull/82) ([stanhu](https://github.com/stanhu)) 30 | 31 | ## [v3.2.0](https://github.com/chef/mixlib-log/tree/v3.2.0) (2025-01-28) 32 | 33 | #### Merged Pull Requests 34 | - Fix formatter regression from Ruby 3.3 Logger fix [#78](https://github.com/chef/mixlib-log/pull/78) ([stanhu](https://github.com/stanhu)) 35 | - Move ffi dependency forward [#81](https://github.com/chef/mixlib-log/pull/81) ([jaymzh](https://github.com/jaymzh)) 36 | 37 | ## [v3.1.1](https://github.com/chef/mixlib-log/tree/v3.1.1) (2024-07-12) 38 | 39 | #### Merged Pull Requests 40 | - Replace __FILE__ with __dir__ and other minor chefstyle fixes [#61](https://github.com/chef/mixlib-log/pull/61) ([tas50](https://github.com/tas50)) 41 | - Add Ruby 3.0 testing [#62](https://github.com/chef/mixlib-log/pull/62) ([tas50](https://github.com/tas50)) 42 | - Upgrade to GitHub-native Dependabot [#63](https://github.com/chef/mixlib-log/pull/63) ([dependabot-preview[bot]](https://github.com/dependabot-preview[bot])) 43 | - Remove Ruby 2.4 support and Test Ruby 3.0/3.1 [#66](https://github.com/chef/mixlib-log/pull/66) ([poorndm](https://github.com/poorndm)) 44 | - Bump minimum to 2.7 [#75](https://github.com/chef/mixlib-log/pull/75) ([tpowell-progress](https://github.com/tpowell-progress)) 45 | - Support ruby3.3 Logger by properly initialize super class [#74](https://github.com/chef/mixlib-log/pull/74) ([mtasaka](https://github.com/mtasaka)) 46 | - ffi less than 1.17.0 because of ruby 3.0 [#77](https://github.com/chef/mixlib-log/pull/77) ([tpowell-progress](https://github.com/tpowell-progress)) 47 | 48 | ## [v3.0.9](https://github.com/chef/mixlib-log/tree/v3.0.9) (2020-08-21) 49 | 50 | #### Merged Pull Requests 51 | - Optimize our requires [#60](https://github.com/chef/mixlib-log/pull/60) ([tas50](https://github.com/tas50)) 52 | 53 | ## [v3.0.8](https://github.com/chef/mixlib-log/tree/v3.0.8) (2019-12-30) 54 | 55 | #### Merged Pull Requests 56 | - Expand testing / Update GitHub templates [#51](https://github.com/chef/mixlib-log/pull/51) ([tas50](https://github.com/tas50)) 57 | - Add Build Kite PR Testing [#52](https://github.com/chef/mixlib-log/pull/52) ([tas50](https://github.com/tas50)) 58 | - Remove Travis PR Testing [#53](https://github.com/chef/mixlib-log/pull/53) ([tas50](https://github.com/tas50)) 59 | - Resolve all warnings emitted during testing [#54](https://github.com/chef/mixlib-log/pull/54) ([zenspider](https://github.com/zenspider)) 60 | - Add windows testing in Buildkite [#56](https://github.com/chef/mixlib-log/pull/56) ([tas50](https://github.com/tas50)) 61 | - Test on Ruby 2.7 + random testing improvements [#58](https://github.com/chef/mixlib-log/pull/58) ([tas50](https://github.com/tas50)) 62 | - Substitute require for require_relative [#59](https://github.com/chef/mixlib-log/pull/59) ([tas50](https://github.com/tas50)) 63 | 64 | ## [v3.0.1](https://github.com/chef/mixlib-log/tree/v3.0.1) (2019-01-05) 65 | 66 | #### Merged Pull Requests 67 | - reverting back to ruby 2.3 support [#48](https://github.com/chef/mixlib-log/pull/48) ([lamont-granquist](https://github.com/lamont-granquist)) 68 | 69 | ## [v3.0.0](https://github.com/chef/mixlib-log/tree/v3.0.0) (2019-01-04) 70 | 71 | #### Merged Pull Requests 72 | - update travis, drop ruby < 2.5, major version bump [#47](https://github.com/chef/mixlib-log/pull/47) ([lamont-granquist](https://github.com/lamont-granquist)) 73 | 74 | ## [v2.0.9](https://github.com/chef/mixlib-log/tree/v2.0.9) (2018-12-18) 75 | 76 | #### Merged Pull Requests 77 | - remove hashrocket syntax [#41](https://github.com/chef/mixlib-log/pull/41) ([lamont-granquist](https://github.com/lamont-granquist)) 78 | - Remove the changelog generator gem [#42](https://github.com/chef/mixlib-log/pull/42) ([tas50](https://github.com/tas50)) 79 | - Test on all the Ruby versions we support [#43](https://github.com/chef/mixlib-log/pull/43) ([tas50](https://github.com/tas50)) 80 | - Test on Ruby 2.6 in Travis + test on Xenial [#46](https://github.com/chef/mixlib-log/pull/46) ([tas50](https://github.com/tas50)) 81 | - Only ship the required library files in the gem artifact [#45](https://github.com/chef/mixlib-log/pull/45) ([tas50](https://github.com/tas50)) 82 | 83 | ## [v2.0.4](https://github.com/chef/mixlib-log/tree/v2.0.4) (2018-04-12) 84 | 85 | #### Merged Pull Requests 86 | - fix labelling [#37](https://github.com/chef/mixlib-log/pull/37) ([thommay](https://github.com/thommay)) 87 | 88 | ## [2.0.1](https://github.com/chef/mixlib-log/tree/2.0.1) (2018-02-28) 89 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v2.0.0...2.0.1) 90 | 91 | **Merged pull requests:** 92 | 93 | - enable metadata to be got and set [\#32](https://github.com/chef/mixlib-log/pull/32) ([thommay](https://github.com/thommay)) 94 | - Logging methods \(debug, info, warn, error, fatal\) all return nil [\#27](https://github.com/chef/mixlib-log/pull/27) ([olleolleolle](https://github.com/olleolleolle)) 95 | 96 | ## [v2.0.0](https://github.com/chef/mixlib-log/tree/v2.0.0) (2018-02-27) 97 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v1.7.1...v2.0.0) 98 | 99 | **Merged pull requests:** 100 | 101 | - Structured Logging [\#30](https://github.com/chef/mixlib-log/pull/30) ([thommay](https://github.com/thommay)) 102 | - RSpec: use 3.7, disable monkey patching mode [\#28](https://github.com/chef/mixlib-log/pull/28) ([olleolleolle](https://github.com/olleolleolle)) 103 | - README: Format a line of code using Markdown, not RDoc [\#24](https://github.com/chef/mixlib-log/pull/24) ([olleolleolle](https://github.com/olleolleolle)) 104 | - Travis: update Ruby versions, pass lint [\#22](https://github.com/chef/mixlib-log/pull/22) ([olleolleolle](https://github.com/olleolleolle)) 105 | - Require Ruby 2.2+ [\#20](https://github.com/chef/mixlib-log/pull/20) ([tas50](https://github.com/tas50)) 106 | 107 | ## [v1.7.1](https://github.com/chef/mixlib-log/tree/v1.7.1) (2016-08-12) 108 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v1.7.0...v1.7.1) 109 | 110 | **Merged pull requests:** 111 | 112 | - Release 1.7.1 [\#19](https://github.com/chef/mixlib-log/pull/19) ([mwrock](https://github.com/mwrock)) 113 | - Allow applications to query if they've got loggers [\#18](https://github.com/chef/mixlib-log/pull/18) ([thommay](https://github.com/thommay)) 114 | 115 | ## [v1.7.0](https://github.com/chef/mixlib-log/tree/v1.7.0) (2016-08-04) 116 | [Full Changelog](https://github.com/chef/mixlib-log/compare/1.4.1...v1.7.0) 117 | 118 | **Merged pull requests:** 119 | 120 | - test with updated ruby versions and run cucumber [\#16](https://github.com/chef/mixlib-log/pull/16) ([thommay](https://github.com/thommay)) 121 | - Add dev dependency on chefstyle [\#15](https://github.com/chef/mixlib-log/pull/15) ([tas50](https://github.com/tas50)) 122 | - Misc cleanup + add Travis [\#14](https://github.com/chef/mixlib-log/pull/14) ([tas50](https://github.com/tas50)) 123 | - File log devices opened by mixlib-log should be closed. [\#13](https://github.com/chef/mixlib-log/pull/13) ([mhorbul](https://github.com/mhorbul)) 124 | - Include the license type in the .gemspec [\#9](https://github.com/chef/mixlib-log/pull/9) ([benders](https://github.com/benders)) 125 | - MIXLIB-10: don't be so pessimistic about development libraries [\#8](https://github.com/chef/mixlib-log/pull/8) ([jkeiser](https://github.com/jkeiser)) 126 | - Ensure that arguments to Mixlib::Log\#add are passed as is to all loggers [\#7](https://github.com/chef/mixlib-log/pull/7) ([ketan](https://github.com/ketan)) 127 | - Fixing RDoc formatting of README.rdoc. [\#4](https://github.com/chef/mixlib-log/pull/4) ([ampledata](https://github.com/ampledata)) 128 | 129 | ## [1.4.1](https://github.com/chef/mixlib-log/tree/1.4.1) (2012-06-08) 130 | [Full Changelog](https://github.com/chef/mixlib-log/compare/1.4.0...1.4.1) 131 | 132 | ## [1.4.0](https://github.com/chef/mixlib-log/tree/1.4.0) (2012-06-08) 133 | [Full Changelog](https://github.com/chef/mixlib-log/compare/1.3.0...1.4.0) 134 | 135 | **Merged pull requests:** 136 | 137 | - Inconsistent documentation about default base log level [\#5](https://github.com/chef/mixlib-log/pull/5) ([bjoernalbers](https://github.com/bjoernalbers)) 138 | 139 | ## [1.3.0](https://github.com/chef/mixlib-log/tree/1.3.0) (2011-03-17) 140 | [Full Changelog](https://github.com/chef/mixlib-log/compare/1.2.0...1.3.0) 141 | 142 | ## [1.2.0](https://github.com/chef/mixlib-log/tree/1.2.0) (2010-10-15) 143 | [Full Changelog](https://github.com/chef/mixlib-log/compare/alpha_deploy_2...1.2.0) 144 | 145 | ## [alpha_deploy_2](https://github.com/chef/mixlib-log/tree/alpha_deploy_2) (2010-02-28) 146 | [Full Changelog](https://github.com/chef/mixlib-log/compare/alpha_deploy_3...alpha_deploy_2) 147 | 148 | ## [alpha_deploy_3](https://github.com/chef/mixlib-log/tree/alpha_deploy_3) (2010-02-28) 149 | [Full Changelog](https://github.com/chef/mixlib-log/compare/alpha_deploy_4...alpha_deploy_3) 150 | 151 | ## [alpha_deploy_4](https://github.com/chef/mixlib-log/tree/alpha_deploy_4) (2010-02-28) 152 | [Full Changelog](https://github.com/chef/mixlib-log/compare/beta-1...alpha_deploy_4) 153 | 154 | ## [beta-1](https://github.com/chef/mixlib-log/tree/beta-1) (2010-02-28) 155 | [Full Changelog](https://github.com/chef/mixlib-log/compare/1.1.0...beta-1) 156 | 157 | ## [1.1.0](https://github.com/chef/mixlib-log/tree/1.1.0) (2010-02-28) 158 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v1.1.0...1.1.0) 159 | 160 | ## [v1.1.0](https://github.com/chef/mixlib-log/tree/v1.1.0) (2010-01-05) 161 | [Full Changelog](https://github.com/chef/mixlib-log/compare/alpha_deploy_1...v1.1.0) 162 | 163 | ## [alpha_deploy_1](https://github.com/chef/mixlib-log/tree/alpha_deploy_1) (2009-05-12) 164 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v1.0.3...alpha_deploy_1) 165 | 166 | ## [v1.0.3](https://github.com/chef/mixlib-log/tree/v1.0.3) (2009-05-12) 167 | [Full Changelog](https://github.com/chef/mixlib-log/compare/v1.0.2...v1.0.3) 168 | 169 | ## [v1.0.2](https://github.com/chef/mixlib-log/tree/v1.0.2) (2009-05-12) 170 | 171 | 172 | \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | Please refer to the Chef Community Code of Conduct at https://www.chef.io/code-of-conduct/ 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please refer to https://github.com/chef/chef/blob/master/CONTRIBUTING.md 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | 2 | source "https://rubygems.org" 3 | 4 | gemspec 5 | 6 | group :test do 7 | gem "activesupport" 8 | gem "cookstyle", ">= 7.32.8" 9 | gem "cucumber", "~> 9.2.1" 10 | gem "rake" 11 | gem "rspec", "~> 3.7" 12 | end 13 | 14 | group :debug do 15 | gem "pry" 16 | gem "pry-byebug" 17 | gem "rb-readline" 18 | end 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ============ 2 | Mixin::Log Notices 3 | ============ 4 | 5 | Developed at Chef (http://www.chef.io). 6 | 7 | 8 | * Copyright 2009-2016, Chef Software, Inc. 9 | 10 | Mixin::Log incorporates code from Chef. The Chef notice file follows: 11 | 12 | ============ 13 | Chef Notices 14 | ============ 15 | 16 | Developed at Chef (http://www.chef.io). 17 | 18 | Contributors and Copyright holders: 19 | 20 | * Copyright 2008, Adam Jacob 21 | * Copyright 2008, Arjuna Christensen 22 | * Copyright 2008, Bryan McLellan 23 | * Copyright 2008, Ezra Zygmuntowicz 24 | * Copyright 2009, Sean Cribbs 25 | * Copyright 2009, Christopher Brown 26 | * Copyright 2009, Thom May 27 | 28 | Chef incorporates code modified from Open4 (http://www.codeforpeople.com/lib/ruby/open4/), which was written by Ara T. Howard. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mixlib::Log 2 | 3 | [![Gem Version](https://badge.fury.io/rb/mixlib-log.svg)](https://badge.fury.io/rb/mixlib-log) 4 | [![Build status](https://badge.buildkite.com/cb1e5b6f3cc77071f4b2315f6b605fe60d86e2862a490873d4.svg?branch=master)](https://buildkite.com/chef-oss/chef-mixlib-log-master-verify) 5 | 6 | **Umbrella Project**: [Chef Foundation](https://github.com/chef/chef-oss-practices/blob/master/projects/chef-foundation.md) 7 | 8 | **Project State**: [Active](https://github.com/chef/chef-oss-practices/blob/master/repo-management/repo-states.md#active) 9 | 10 | **Issues [Response Time Maximum](https://github.com/chef/chef-oss-practices/blob/master/repo-management/repo-states.md)**: 14 days 11 | 12 | **Pull Request [Response Time Maximum](https://github.com/chef/chef-oss-practices/blob/master/repo-management/repo-states.md)**: 14 days 13 | 14 | Mixlib::Log provides a mixin for enabling a class based logger object, a-la Merb, Chef, and Nanite. To use it: 15 | 16 | ```ruby 17 | require 'mixlib/log' 18 | 19 | class Log 20 | extend Mixlib::Log 21 | end 22 | ``` 23 | 24 | You can then do: 25 | 26 | ```ruby 27 | Log.debug('foo') 28 | Log.info('bar') 29 | Log.warn('baz') 30 | Log.error('baz') 31 | Log.fatal('wewt') 32 | ``` 33 | 34 | By default, `Mixlib::Logger` logs to STDOUT. To alter this, you should call `Log.init`, passing any arguments to the standard Ruby Logger. For example: 35 | 36 | ```ruby 37 | Log.init('/tmp/logfile') # log to /tmp/logfile 38 | Log.init('/tmp/logfile', 7) # log to /tmp/logfile, rotate every day 39 | ``` 40 | 41 | Enjoy! 42 | 43 | ## Documentation 44 | 45 | All documentation is written using YARD. You can generate a by running: 46 | 47 | ``` 48 | rake docs 49 | ``` 50 | 51 | ## Contributing 52 | 53 | For information on contributing to this project please see our [Contributing Documentation](https://github.com/chef/chef/blob/master/CONTRIBUTING.md) 54 | 55 | ## License & Copyright 56 | 57 | - Copyright:: Copyright (c) 2008-2019 Chef Software, Inc. 58 | - License:: Apache License, Version 2.0 59 | 60 | ```text 61 | Licensed under the Apache License, Version 2.0 (the "License"); 62 | you may not use this file except in compliance with the License. 63 | You may obtain a copy of the License at 64 | 65 | http://www.apache.org/licenses/LICENSE-2.0 66 | 67 | Unless required by applicable law or agreed to in writing, software 68 | distributed under the License is distributed on an "AS IS" BASIS, 69 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 70 | See the License for the specific language governing permissions and 71 | limitations under the License. 72 | ``` 73 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler" 2 | 3 | begin 4 | require "cucumber/rake/task" 5 | 6 | Cucumber::Rake::Task.new(:features) do |t| 7 | t.cucumber_opts = "--format pretty" 8 | t.bundler = false 9 | end 10 | rescue LoadError 11 | desc "cucumber is not installed, this task is disabled" 12 | task :spec do 13 | abort "cucumber is not installed. bundle install first to make sure all dependencies are installed." 14 | end 15 | end 16 | 17 | begin 18 | require "rspec/core/rake_task" 19 | 20 | RSpec::Core::RakeTask.new do |t| 21 | t.pattern = "spec/**/*_spec.rb" 22 | end 23 | rescue LoadError 24 | desc "rspec is not installed, this task is disabled" 25 | task :spec do 26 | abort "rspec is not installed. bundle install first to make sure all dependencies are installed." 27 | end 28 | end 29 | 30 | begin 31 | require "cookstyle/chefstyle" 32 | require "rubocop/rake_task" 33 | desc "Run Chefstyle tests" 34 | RuboCop::RakeTask.new(:style) do |task| 35 | task.options += ["--display-cop-names", "--no-color"] 36 | end 37 | rescue LoadError 38 | puts "cookstyle gem is not installed. bundle install first to make sure all dependencies are installed." 39 | end 40 | 41 | task :console do 42 | require "irb" 43 | require "irb/completion" 44 | require "mixlib/log" 45 | ARGV.clear 46 | IRB.start 47 | end 48 | 49 | task default: %i{style spec features} 50 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 3.2.5 -------------------------------------------------------------------------------- /features/log.feature: -------------------------------------------------------------------------------- 1 | Feature: Log output 2 | In order to keep a record of application specific information 3 | As a developer 4 | I want to publish information through a configurable log interface 5 | 6 | Scenario: Log a message at the debug level 7 | Given a base log level of 'debug' 8 | When the message 'this goes out' is sent at the 'debug' level 9 | Then the regex '\[.+\] DEBUG: this goes out' should be logged 10 | 11 | Scenario: Log a message at the info level 12 | Given a base log level of 'info' 13 | When the message 'this goes out' is sent at the 'info' level 14 | Then the regex '\[.+\] INFO: this goes out' should be logged 15 | 16 | Scenario: Log a message at the warn level 17 | Given a base log level of 'warn' 18 | When the message 'this goes out' is sent at the 'warn' level 19 | Then the regex '\[.+\] WARN: this goes out' should be logged 20 | 21 | Scenario: Log a message at the error level 22 | Given a base log level of 'error' 23 | When the message 'this goes out' is sent at the 'error' level 24 | Then the regex '\[.+\] ERROR: this goes out' should be logged 25 | 26 | Scenario: Log a message at the fatal level 27 | Given a base log level of 'fatal' 28 | When the message 'this goes out' is sent at the 'fatal' level 29 | Then the regex '\[.+\] FATAL: this goes out' should be logged 30 | 31 | Scenario: Log messages below the current threshold should not appear 32 | Given a base log level of 'fatal' 33 | When the message 'this goes out' is sent at the 'error' level 34 | And the message 'this goes out' is sent at the 'warn' level 35 | And the message 'this goes out' is sent at the 'info' level 36 | And the message 'this goes out' is sent at the 'debug' level 37 | Then nothing should be logged 38 | -------------------------------------------------------------------------------- /features/steps/log.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 4 | # License:: Apache License, Version 2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | Given(/^a base log level of '(.+)'$/) do |level| 20 | Logit.level = level.to_sym 21 | end 22 | 23 | When(/^the message '(.+)' is sent at the '(.+)' level$/) do |message, level| 24 | case level.to_sym 25 | when :debug 26 | Logit.debug(message) 27 | when :info 28 | Logit.info(message) 29 | when :warn 30 | Logit.warn(message) 31 | when :error 32 | Logit.error(message) 33 | when :fatal 34 | Logit.fatal(message) 35 | else 36 | raise ArgumentError, "Level is not one of debug, info, warn, error, or fatal" 37 | end 38 | end 39 | 40 | Then(/^the regex '(.+)' should be logged$/) do |regex_string| 41 | regex = Regexp.new(regex_string, Regexp::MULTILINE) 42 | expect(regex.match(@output)).not_to be_nil 43 | end 44 | 45 | Then(/^nothing should be logged$/) do 46 | expect(@output).to eq "" 47 | end 48 | -------------------------------------------------------------------------------- /features/support/env.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 4 | # License:: Apache License, Version 2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | $: << File.join(File.dirname(__FILE__), "..", "..", "lib") 20 | 21 | require "rspec/expectations" 22 | require "mixlib/log" 23 | require "tmpdir" 24 | require "stringio" 25 | 26 | class MyWorld 27 | def initialize 28 | @tmpdir = File.join(Dir.tmpdir, "mixlib_log") 29 | @output = "" 30 | @output_io = StringIO.new(@output) 31 | Logit.init(@output_io) 32 | end 33 | end 34 | 35 | World do 36 | MyWorld.new 37 | end 38 | 39 | Before do 40 | system("mkdir -p #{@tmpdir}") 41 | end 42 | 43 | After do 44 | system("rm -rf #{@tmpdir}") 45 | end 46 | -------------------------------------------------------------------------------- /features/support/logit.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 4 | # License:: Apache License, Version 2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | class Logit 20 | extend Mixlib::Log 21 | end 22 | -------------------------------------------------------------------------------- /lib/mixlib/log.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Author:: Christopher Brown () 4 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 5 | # License:: Apache License, Version 2.0 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | require "logger" 20 | require_relative "log/version" 21 | require_relative "log/formatter" 22 | require_relative "log/child" 23 | require_relative "log/logging" 24 | require_relative "log/logger" 25 | 26 | module Mixlib 27 | module Log 28 | 29 | include Logging 30 | 31 | def reset! 32 | @logger ||= nil 33 | @loggers ||= [] 34 | close! 35 | @logger = @loggers = nil 36 | @metadata = {} 37 | end 38 | 39 | # An Array of log devices that will be logged to. Defaults to just the default 40 | # \@logger log device, but you can push to this array to add more devices. 41 | def loggers 42 | @loggers ||= [logger] 43 | end 44 | 45 | ## 46 | # init always returns a configured logger 47 | # and creates a new one if it doesn't yet exist 48 | ## 49 | def logger 50 | @logger ||= init 51 | end 52 | 53 | # Sets the log device to +new_log_device+. Any additional loggers 54 | # that had been added to the +loggers+ array will be cleared. 55 | def logger=(new_log_device) 56 | reset! 57 | @logger = new_log_device 58 | end 59 | 60 | def use_log_devices(other) 61 | if other.respond_to?(:loggers) && other.respond_to?(:logger) 62 | @loggers = other.loggers 63 | @logger = other.logger 64 | elsif other.is_a?(Array) 65 | @loggers = other 66 | @logger = other.first 67 | else 68 | msg = "#use_log_devices takes a Mixlib::Log object or array of log devices. " << 69 | "You gave: #{other.inspect}" 70 | raise ArgumentError, msg 71 | end 72 | @configured = true 73 | end 74 | 75 | # Use Mixlib::Log.init when you want to set up the logger manually. Arguments to this method 76 | # get passed directly to Logger.new, so check out the documentation for the standard Logger class 77 | # to understand what to do here. 78 | # 79 | # If this method is called with no arguments, it will log to STDOUT at the :warn level. 80 | # 81 | # It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class. 82 | def init(*opts) 83 | reset! 84 | @logger = logger_for(*opts) 85 | @logger.formatter = Mixlib::Log::Formatter.new if @logger.respond_to?(:formatter=) 86 | @logger.level = Logger::WARN 87 | @configured = true 88 | @parent = nil 89 | @metadata = {} 90 | @logger 91 | end 92 | 93 | # Let the application query if logging objects have been set up 94 | def configured? 95 | @configured 96 | end 97 | 98 | attr_accessor :metadata 99 | 100 | # Sets the level for the Logger object by symbol. Valid arguments are: 101 | # 102 | # :trace 103 | # :debug 104 | # :info 105 | # :warn 106 | # :error 107 | # :fatal 108 | # 109 | # Throws an ArgumentError if you feed it a bogus log level. 110 | def level=(new_level) 111 | level_int = LEVEL_NAMES.key?(new_level) ? new_level : LEVELS[new_level] 112 | raise ArgumentError, "Log level must be one of :trace, :debug, :info, :warn, :error, or :fatal" if level_int.nil? 113 | 114 | loggers.each { |l| l.level = level_int } 115 | end 116 | 117 | def level(new_level = nil) 118 | if new_level.nil? 119 | LEVEL_NAMES[logger.level] 120 | else 121 | self.level = new_level 122 | end 123 | end 124 | 125 | # Define the methods to interrogate the logger for the current log level. 126 | # Note that we *only* query the default logger (@logger) and not any other 127 | # loggers that may have been added, even though it is possible to configure 128 | # two (or more) loggers at different log levels. 129 | %i{trace? debug? info? warn? error? fatal?}.each do |method_name| 130 | define_method(method_name) do 131 | logger.send(method_name) 132 | end 133 | end 134 | 135 | def <<(msg) 136 | loggers.each { |l| l << msg } 137 | end 138 | 139 | def add(severity, message = nil, progname = nil, data: {}, &block) 140 | message, progname, data = yield if block_given? 141 | data = metadata.merge(data) if metadata.is_a?(Hash) && data.is_a?(Hash) 142 | loggers.each do |l| 143 | # if we don't have any metadata, let's not do the potentially expensive 144 | # merging and managing that this call requires 145 | if l.respond_to?(:add_data) && !data.nil? && !data.empty? 146 | l.add_data(severity, message, progname, data: data) 147 | else 148 | l.add(severity, message, progname) 149 | end 150 | end 151 | end 152 | 153 | alias :log :add 154 | 155 | def with_child(metadata = {}) 156 | child = Child.new(self, metadata) 157 | if block_given? 158 | yield child 159 | else 160 | child 161 | end 162 | end 163 | 164 | # Passes any other method calls on directly to the underlying Logger object created with init. If 165 | # this method gets hit before a call to Mixlib::Logger.init has been made, it will call 166 | # Mixlib::Logger.init() with no arguments. 167 | def method_missing(method_symbol, *args, &block) 168 | loggers.each { |l| l.send(method_symbol, *args, &block) } 169 | end 170 | 171 | private 172 | 173 | def logger_for(*opts) 174 | if opts.empty? 175 | Mixlib::Log::Logger.new($stdout) 176 | elsif LEVELS.keys.inject(true) { |quacks, level| quacks && opts.first.respond_to?(level) } 177 | opts.first 178 | else 179 | Mixlib::Log::Logger.new(*opts) 180 | end 181 | end 182 | 183 | def all_loggers 184 | [@logger, *@loggers].uniq 185 | end 186 | 187 | # select all loggers with File log devices 188 | def loggers_to_close 189 | loggers_to_close = [] 190 | all_loggers.each do |logger| 191 | # unfortunately Logger does not provide access to the logdev 192 | # via public API. In order to reduce amount of impact and 193 | # handle only File type log devices I had to use this method 194 | # to get access to it. 195 | next unless logger.instance_variable_defined?(:"@logdev") 196 | next unless (logdev = logger.instance_variable_get(:"@logdev")) 197 | 198 | loggers_to_close << logger if logdev.filename 199 | end 200 | loggers_to_close 201 | end 202 | 203 | def close! 204 | # try to close all file loggers 205 | loggers_to_close.each do |l| 206 | l.close rescue nil 207 | end 208 | end 209 | 210 | end 211 | end 212 | -------------------------------------------------------------------------------- /lib/mixlib/log/child.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright:: Copyright (c) 2018 Chef Software, Inc. 3 | # License:: Apache License, Version 2.0 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | require_relative "logging" 18 | 19 | module Mixlib 20 | module Log 21 | class Child 22 | include Mixlib::Log::Logging 23 | 24 | attr_reader :parent 25 | attr_accessor :metadata 26 | def initialize(parent, metadata = {}) 27 | @parent = parent 28 | @metadata = metadata 29 | end 30 | 31 | def level 32 | parent.level 33 | end 34 | 35 | # Define the methods to interrogate the logger for the current log level. 36 | # Note that we *only* query the default logger (@logger) and not any other 37 | # loggers that may have been added, even though it is possible to configure 38 | # two (or more) loggers at different log levels. 39 | %i{trace? debug? info? warn? error? fatal?}.each do |method_name| 40 | define_method(method_name) do 41 | parent.send(method_name) 42 | end 43 | end 44 | 45 | def add(severity, message = nil, progname = nil, data: {}, &block) 46 | data = metadata.merge(data) if data.is_a?(Hash) 47 | parent.send(:pass, severity, message, progname, data: data, &block) 48 | end 49 | 50 | def with_child(metadata = {}) 51 | child = Child.new(self, metadata) 52 | if block_given? 53 | yield child 54 | else 55 | child 56 | end 57 | end 58 | 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /lib/mixlib/log/formatter.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 4 | # License:: Apache License, Version 2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | require "logger" 19 | require "time" unless defined?(Time.zone_offset) 20 | 21 | module Mixlib 22 | module Log 23 | class Formatter < Logger::Formatter 24 | @@show_time = true 25 | 26 | def self.show_time=(show = false) 27 | @@show_time = show 28 | end 29 | 30 | # Prints a log message as '[time] severity: message' if Chef::Log::Formatter.show_time == true. 31 | # Otherwise, doesn't print the time. 32 | def call(severity, time, progname, msg) 33 | if @@show_time 34 | sprintf("[%s] %s: %s\n", time.iso8601, severity, msg2str(msg)) 35 | else 36 | sprintf("%s: %s\n", severity, msg2str(msg)) 37 | end 38 | end 39 | 40 | # Converts some argument to a Logger.severity() call to a string. Regular strings pass through like 41 | # normal, Exceptions get formatted as "message (class)\nbacktrace", and other random stuff gets 42 | # put through "object.inspect" 43 | def msg2str(msg) 44 | case msg 45 | when ::Hash 46 | if msg.has_key?(:err) 47 | format_exception(msg[:err]) 48 | else 49 | msg[:msg] 50 | end 51 | when ::String 52 | msg 53 | when ::Exception 54 | format_exception(msg) 55 | else 56 | msg.inspect 57 | end 58 | end 59 | 60 | def format_exception(msg) 61 | "#{msg.message} (#{msg.class})\n" << 62 | (msg.backtrace || []).join("\n") 63 | end 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/mixlib/log/logger.rb: -------------------------------------------------------------------------------- 1 | require "logger" 2 | require_relative "logging" 3 | 4 | # A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff 5 | # ripped out, and metadata added in. 6 | module Mixlib 7 | module Log 8 | class Logger < ::Logger 9 | 10 | include Logging 11 | 12 | def trace?; @level <= TRACE; end 13 | 14 | # 15 | # === Synopsis 16 | # 17 | # Logger.new(name, shift_age = 7, shift_size = 1048576) 18 | # Logger.new(name, shift_age = 'weekly') 19 | # 20 | # === Args 21 | # 22 | # +logdev+:: 23 | # The log device. This is a filename (String) or IO object (typically 24 | # +$stdout+, +$stderr+, or an open file). 25 | # +shift_age+:: 26 | # Number of old log files to keep, *or* frequency of rotation (+daily+, 27 | # +weekly+ or +monthly+). 28 | # +shift_size+:: 29 | # Maximum logfile size (only applies when +shift_age+ is a number). 30 | # 31 | # === Description 32 | # 33 | # Create an instance. 34 | # 35 | def initialize(logdev) 36 | super(nil, formatter: ::Mixlib::Log::Formatter.new) 37 | if logdev 38 | @logdev = LocklessLogDevice.new(logdev) 39 | end 40 | end 41 | 42 | def add_data(severity, message, progname, data: {}) 43 | return true if @logdev.nil? || severity < @level 44 | 45 | data ||= {} 46 | if message.is_a?(::Exception) 47 | data[:err] = message 48 | else 49 | data[:msg] = message 50 | end 51 | @logdev.write( 52 | format_message(to_label(severity), Time.now, progname, data) 53 | ) 54 | true 55 | end 56 | alias_method :add, :add_data 57 | 58 | class LocklessLogDevice < LogDevice 59 | 60 | def initialize(log = nil) 61 | @dev = @filename = @shift_age = @shift_size = nil 62 | if log.respond_to?(:write) && log.respond_to?(:close) 63 | @dev = log 64 | else 65 | @dev = open_logfile(log) 66 | @filename = log 67 | end 68 | @dev.sync = true 69 | end 70 | 71 | def write(message) 72 | @dev.write(message) 73 | rescue Exception => ignored 74 | warn("log writing failed. #{ignored}") 75 | end 76 | 77 | def close 78 | @dev.close rescue nil 79 | end 80 | 81 | private 82 | 83 | def open_logfile(filename) 84 | if FileTest.exist?(filename) 85 | open(filename, (File::WRONLY | File::APPEND)) 86 | else 87 | create_logfile(filename) 88 | end 89 | end 90 | 91 | def create_logfile(filename) 92 | logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT)) 93 | add_log_header(logdev) 94 | logdev 95 | end 96 | 97 | def add_log_header(file) 98 | file.write( 99 | "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName] 100 | ) 101 | end 102 | 103 | end 104 | 105 | end 106 | end 107 | end 108 | -------------------------------------------------------------------------------- /lib/mixlib/log/logging.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright:: Copyright (c) 2018 Chef Software, Inc. 3 | # License:: Apache License, Version 2.0 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | require "logger" 18 | 19 | module Mixlib 20 | module Log 21 | module Logging 22 | include ::Logger::Severity 23 | 24 | TRACE = -1 25 | 26 | SEV_LABEL = %w{TRACE DEBUG INFO WARN ERROR FATAL ANY}.each(&:freeze).freeze 27 | 28 | def to_label(sev) 29 | SEV_LABEL[sev + 1] || -"ANY" 30 | end 31 | 32 | LEVELS = { trace: TRACE, debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL }.freeze 33 | LEVEL_NAMES = LEVELS.invert.freeze 34 | 35 | attr_accessor :metadata 36 | 37 | def pass(severity, args, progname = nil, data: {}, &block) 38 | args, progname, data = yield if block_given? 39 | add(severity, args, progname, data: data) 40 | end 41 | 42 | # Define the standard logger methods on this class programmatically. 43 | # No need to incur method_missing overhead on every log call. 44 | %i{trace debug info warn error fatal}.each do |method_name| 45 | level = LEVELS[method_name] 46 | define_method(method_name) do |msg = nil, data: {}, &block| 47 | pass(level, msg, data: data, &block) 48 | nil 49 | end 50 | end 51 | 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/mixlib/log/version.rb: -------------------------------------------------------------------------------- 1 | module Mixlib 2 | module Log 3 | VERSION = "3.2.5".freeze 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /mixlib-log.gemspec: -------------------------------------------------------------------------------- 1 | $:.unshift File.expand_path("lib", __dir__) 2 | require "mixlib/log/version" 3 | 4 | Gem::Specification.new do |gem| 5 | gem.name = "mixlib-log" 6 | gem.version = Mixlib::Log::VERSION 7 | gem.summary = "A gem that provides a simple mixin for log functionality" 8 | gem.email = "info@chef.io" 9 | gem.homepage = "https://github.com/chef/mixlib-log" 10 | gem.license = "Apache-2.0" 11 | gem.authors = ["Chef Software, Inc."] 12 | gem.files = %w{LICENSE} + Dir.glob("lib/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) } 13 | gem.require_paths = ["lib"] 14 | gem.required_ruby_version = ">= 3.1" 15 | 16 | gem.add_dependency "ffi", ">= 1.15.5" 17 | end 18 | -------------------------------------------------------------------------------- /spec/mixlib/log/child_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright:: Copyright (c) 2018 Chef Software, Inc. 3 | # License:: Apache License, Version 2.0 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | require "tempfile" unless defined?(Tempfile) 19 | require "stringio" unless defined?(StringIO) 20 | require "spec_helper" 21 | 22 | RSpec.describe Mixlib::Log::Child do 23 | before do 24 | Logit.reset! 25 | Logit.init(io) 26 | Logit.level = :warn 27 | end 28 | 29 | let(:io) { StringIO.new } 30 | 31 | let(:child) { Logit.with_child } 32 | 33 | it "has a parent" do 34 | expect(child.parent).to be(Logit) 35 | end 36 | 37 | it "accepts a message" do 38 | Logit.with_child { |l| l.add(Logger::WARN, "a message") } 39 | expect(io.string).to match(/a message$/) 40 | end 41 | 42 | context "with structured data" do 43 | it "can be created with metadata" do 44 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true" }) 45 | Logit.with_child({ child: "true" }) { |l| l.warn("a message") } 46 | end 47 | 48 | it "a message can be logged" do 49 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true" }) 50 | Logit.with_child { |l| l.warn("a message", data: { child: "true" }) } 51 | end 52 | 53 | context "merges properly" do 54 | it "in the simple case" do 55 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", meta: "data" }) 56 | Logit.with_child(meta: "data") { |l| l.warn("a message", data: { child: "true" }) } 57 | end 58 | 59 | it "when overwriting" do 60 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", meta: "overwritten" }) 61 | Logit.with_child(meta: "data") { |l| l.warn("a message", data: { child: "true", meta: "overwritten" }) } 62 | end 63 | end 64 | 65 | context "when receiving a message from a child" do 66 | it "passes data on" do 67 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", parent: "first" }) 68 | child.metadata = { parent: "first" } 69 | child.with_child { |l| l.warn("a message", data: { child: "true" }) } 70 | end 71 | 72 | it "merges its own data" do 73 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[:warn], "a message", nil, data: { child: "true", parent: "second" }) 74 | child.metadata = { parent: "first" } 75 | child.with_child { |l| l.warn("a message", data: { child: "true", parent: "second" }) } 76 | end 77 | end 78 | end 79 | 80 | context "sends a message to the parent" do 81 | %i{ debug info warn error fatal }.each do |level| 82 | it "at #{level}" do 83 | expect(Logit).to receive(:pass).with(Mixlib::Log::LEVELS[level], "a #{level} message", nil, data: {}) 84 | Logit.level = level 85 | child.send(level, "a #{level} message") 86 | end 87 | end 88 | end 89 | 90 | context "can query the parent's level" do 91 | %i{ debug info warn error fatal }.each do |level| 92 | it "at #{level}" do 93 | query = "#{level}?".to_sym 94 | Logit.level = level 95 | expect(child.send(query)).to be(true) 96 | end 97 | end 98 | end 99 | end 100 | -------------------------------------------------------------------------------- /spec/mixlib/log/formatter_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 4 | # License:: Apache License, Version 2.0 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | require "time" unless defined?(Time.zone_offset) 20 | require "spec_helper" 21 | 22 | RSpec.describe Mixlib::Log::Formatter do 23 | before(:each) do 24 | @formatter = Mixlib::Log::Formatter.new 25 | end 26 | 27 | it "should print raw strings with msg2str(string)" do 28 | expect(@formatter.msg2str("nuthin new")).to eq("nuthin new") 29 | end 30 | 31 | it "should format exceptions properly with msg2str(e)" do 32 | e = IOError.new("legendary roots crew") 33 | expect(@formatter.msg2str(e)).to eq("legendary roots crew (IOError)\n") 34 | end 35 | 36 | it "should format random objects via inspect with msg2str(Object)" do 37 | expect(@formatter.msg2str([ "black thought", "?uestlove" ])).to eq('["black thought", "?uestlove"]') 38 | end 39 | 40 | it "should return a formatted string with call" do 41 | time = Time.new 42 | Mixlib::Log::Formatter.show_time = true 43 | expect(@formatter.call("monkey", time, "test", "mos def")).to eq("[#{time.iso8601}] monkey: mos def\n") 44 | end 45 | 46 | it "should allow you to turn the time on and off in the output" do 47 | Mixlib::Log::Formatter.show_time = false 48 | expect(@formatter.call("monkey", Time.new, "test", "mos def")).to eq("monkey: mos def\n") 49 | end 50 | 51 | context "with structured data" do 52 | let(:data) { {} } 53 | 54 | it "should format a message" do 55 | data[:msg] = "nuthin new" 56 | expect(@formatter.msg2str(data)).to eq("nuthin new") 57 | end 58 | 59 | it "should format an exception" do 60 | data[:err] = IOError.new("legendary roots crew") 61 | expect(@formatter.msg2str(data)).to eq("legendary roots crew (IOError)\n") 62 | end 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /spec/mixlib/log/logger_spec.rb: -------------------------------------------------------------------------------- 1 | # Copyright:: Copyright (c) 2024 Chef Software, Inc. 2 | # License:: Apache License, Version 2.0 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | require "spec_helper" 18 | 19 | RSpec.describe Mixlib::Log::Logger do 20 | let(:io) { StringIO.new } 21 | 22 | subject { described_class.new(io) } 23 | 24 | it "logs a info message in text format" do 25 | subject.info("test message") 26 | 27 | expect(io.string).to match(/INFO: test message/) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /spec/mixlib/log_spec.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Author:: Christopher Brown () 4 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 5 | # License:: Apache License, Version 2.0 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | require "tempfile" unless defined?(Tempfile) 21 | require "stringio" unless defined?(StringIO) 22 | require "spec_helper" 23 | 24 | class LoggerLike 25 | attr_accessor :level 26 | attr_reader :messages, :data 27 | def initialize 28 | @messages = "" 29 | @data = [] 30 | end 31 | 32 | def add_data(severity, message = nil, progname = nil, data: {}) 33 | @messages << message 34 | @data << data 35 | end 36 | 37 | def add(severity, message = nil, progname = nil, data: {}) 38 | @messages << message 39 | end 40 | 41 | %i{trace debug info warn error fatal}.each do |method_name| 42 | class_eval(<<-E) 43 | def #{method_name}(message) 44 | @messages << message 45 | end 46 | E 47 | end 48 | end 49 | 50 | RSpec.describe Mixlib::Log do 51 | 52 | # Since we are testing class behaviour for an instance variable 53 | # that gets set once, we need to reset it prior to each example [cb] 54 | before(:each) do 55 | Logit.reset! 56 | end 57 | 58 | it "creates a logger using an IO object" do 59 | io = StringIO.new 60 | Logit.init(io) 61 | Logit << "foo" 62 | expect(io.string).to match(/foo/) 63 | end 64 | 65 | it "creates a logger with a file name" do 66 | Tempfile.open("chef-test-log") do |tempfile| 67 | Logit.init(tempfile.path) 68 | Logit << "bar" 69 | tempfile.rewind 70 | expect(tempfile.read).to match(/bar/) 71 | end 72 | end 73 | 74 | it "uses the logger provided when initialized with a logger like object" do 75 | logger = LoggerLike.new 76 | Logit.init(logger) 77 | Logit.level = :debug 78 | Logit.debug "qux" 79 | expect(logger.messages).to match(/qux/) 80 | end 81 | 82 | it "should re-initialize the logger if init is called again" do 83 | first_logdev, second_logdev = StringIO.new, StringIO.new 84 | Logit.init(first_logdev) 85 | Logit.fatal "FIRST" 86 | expect(first_logdev.string).to match(/FIRST/) 87 | Logit.init(second_logdev) 88 | Logit.fatal "SECOND" 89 | expect(first_logdev.string).to_not match(/SECOND/) 90 | expect(second_logdev.string).to match(/SECOND/) 91 | end 92 | 93 | it "knows that it's been configured" do 94 | Logit.init 95 | expect(Logit.configured?).to be true 96 | end 97 | 98 | it "should set the log level using the binding form, with :trace, :debug, :info, :warn, :error, or :fatal" do 99 | levels = { 100 | trace: Mixlib::Log::TRACE, 101 | debug: Mixlib::Log::DEBUG, 102 | info: Mixlib::Log::INFO, 103 | warn: Mixlib::Log::WARN, 104 | error: Mixlib::Log::ERROR, 105 | fatal: Mixlib::Log::FATAL, 106 | } 107 | levels.each do |symbol, constant| 108 | Logit.level = symbol 109 | expect(Logit.logger.level).to eq(constant) 110 | expect(Logit.level).to eq(symbol) 111 | end 112 | end 113 | 114 | it "passes blocks to the underlying logger object" do 115 | logdev = StringIO.new 116 | Logit.init(logdev) 117 | Logit.fatal { "the_message" } 118 | expect(logdev.string).to match(/the_message/) 119 | end 120 | 121 | it "should set the log level using the method form, with :trace, :debug, :info, :warn, :error, or :fatal" do 122 | levels = { 123 | trace: Mixlib::Log::TRACE, 124 | debug: Mixlib::Log::DEBUG, 125 | info: Mixlib::Log::INFO, 126 | warn: Mixlib::Log::WARN, 127 | error: Mixlib::Log::ERROR, 128 | fatal: Mixlib::Log::FATAL, 129 | } 130 | levels.each do |symbol, constant| 131 | Logit.level(symbol) 132 | expect(Logit.logger.level).to eq(constant) 133 | end 134 | end 135 | 136 | it "should raise an ArgumentError if you try and set the level to something strange using the binding form" do 137 | expect { Logit.level = :the_roots }.to raise_error(ArgumentError) 138 | end 139 | 140 | it "should raise an ArgumentError if you try and set the level to something strange using the method form" do 141 | expect { Logit.level(:the_roots) }.to raise_error(ArgumentError) 142 | end 143 | 144 | it "should pass other method calls directly to logger" do 145 | expect do 146 | # this needs to be inside of the block because the level setting 147 | # is causing the init, which grabs $stderr before rspec replaces 148 | # it for output testing. 149 | Logit.level = :debug 150 | expect(Logit).to be_debug 151 | Logit.debug("Gimme some sugar!") 152 | end.to output(/DEBUG: Gimme some sugar!/).to_stdout 153 | end 154 | 155 | it "should pass add method calls directly to logger" do 156 | logdev = StringIO.new 157 | Logit.init(logdev) 158 | Logit.level = :debug 159 | expect(Logit).to be_debug 160 | expect { Logit.add(Logger::DEBUG, "Gimme some sugar!") }.to_not raise_error 161 | expect(logdev.string).to match(/Gimme some sugar/) 162 | end 163 | 164 | it "should default to STDOUT if init is called with no arguments" do 165 | logger_mock = Struct.new(:formatter, :level).new 166 | # intentionally STDOUT to avoid unfailable test 167 | expect(Logger).to receive(:new).with(STDOUT).and_return(logger_mock) 168 | Logit.init 169 | end 170 | 171 | it "should have by default a base log level of warn" do 172 | logger_mock = Struct.new(:formatter, :level).new 173 | expect(Logger).to receive(:new).and_return(logger_mock) 174 | Logit.init 175 | expect(Logit.level).to eq(:warn) 176 | end 177 | 178 | it "should close File logger" do 179 | opened_files_count_before = 0 180 | ObjectSpace.each_object(File) do |f| 181 | opened_files_count_before += 1 unless f.closed? 182 | end 183 | name = File.join(Dir.tmpdir, "logger.log") 184 | Logit.init(name) 185 | Logit.init(name) 186 | Logit.init(name) 187 | opened_files_count_after = 0 188 | ObjectSpace.each_object(File) do |f| 189 | opened_files_count_after += 1 unless f.closed? 190 | end 191 | expect(opened_files_count_after).to eq(opened_files_count_before + 1) 192 | end 193 | 194 | it "should not close IO logger" do 195 | opened_files_count_before = 0 196 | ObjectSpace.each_object(File) do |f| 197 | opened_files_count_before += 1 unless f.closed? 198 | end 199 | Tempfile.open("chef-test-log") do |file| 200 | Logit.init(file) 201 | Logit.init(file) 202 | Logit.init(file) 203 | opened_files_count_after = 0 204 | ObjectSpace.each_object(File) do |f| 205 | opened_files_count_after += 1 unless f.closed? 206 | end 207 | expect(opened_files_count_after).to eq(opened_files_count_before + 1) 208 | end 209 | end 210 | 211 | it "should return nil from its logging methods" do 212 | # intentionally STDOUT to avoid unfailable test 213 | expect(Logger).to receive(:new).with(STDOUT) { double("a-quiet-logger").as_null_object } 214 | Logit.init 215 | 216 | aggregate_failures "returns nil from logging method" do 217 | expect(Logit.trace("hello")).to be_nil 218 | expect(Logit.debug("hello")).to be_nil 219 | expect(Logit.info("hello")).to be_nil 220 | expect(Logit.warn("hello")).to be_nil 221 | expect(Logit.error("hello")).to be_nil 222 | expect(Logit.fatal("hello")).to be_nil 223 | end 224 | end 225 | 226 | it "should set metadata correctly" do 227 | Logit.metadata = { test: "data" } 228 | expect(Logit.metadata).to eql({ test: "data" }) 229 | end 230 | 231 | it "should format :trace level messages with TRACE: label" do 232 | logdev = StringIO.new 233 | Logit.init(logdev) 234 | Logit.level = :trace 235 | Logit.trace("this is a log message") 236 | aggregate_failures do 237 | expect(logdev.string).to_not match(/ANY:/) 238 | expect(logdev.string).to match(/TRACE:/) 239 | end 240 | end 241 | end 242 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Author:: Adam Jacob () 3 | # Author:: Christopher Brown () 4 | # Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. 5 | # License:: Apache License, Version 2.0 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | $TESTING = true 21 | 22 | require "rspec" 23 | require "mixlib/log" 24 | require "mixlib/log/formatter" 25 | 26 | RSpec.configure(&:disable_monkey_patching!) 27 | 28 | class Logit 29 | extend Mixlib::Log 30 | end 31 | --------------------------------------------------------------------------------