You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
1.9 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package log provides a universal logger for martian packages.
  15. package log
  16. import (
  17. "fmt"
  18. "log"
  19. "sync"
  20. )
  21. const (
  22. // Silent is a level that logs nothing.
  23. Silent int = iota
  24. // Error is a level that logs error logs.
  25. Error
  26. // Info is a level that logs error, and info logs.
  27. Info
  28. // Debug is a level that logs error, info, and debug logs.
  29. Debug
  30. )
  31. // Default log level is Error.
  32. var (
  33. level = Error
  34. lock sync.Mutex
  35. )
  36. // SetLevel sets the global log level.
  37. func SetLevel(l int) {
  38. lock.Lock()
  39. defer lock.Unlock()
  40. level = l
  41. }
  42. // Infof logs an info message.
  43. func Infof(format string, args ...interface{}) {
  44. lock.Lock()
  45. defer lock.Unlock()
  46. if level < Info {
  47. return
  48. }
  49. msg := fmt.Sprintf("INFO: %s", format)
  50. if len(args) > 0 {
  51. msg = fmt.Sprintf(msg, args...)
  52. }
  53. log.Println(msg)
  54. }
  55. // Debugf logs a debug message.
  56. func Debugf(format string, args ...interface{}) {
  57. lock.Lock()
  58. defer lock.Unlock()
  59. if level < Debug {
  60. return
  61. }
  62. msg := fmt.Sprintf("DEBUG: %s", format)
  63. if len(args) > 0 {
  64. msg = fmt.Sprintf(msg, args...)
  65. }
  66. log.Println(msg)
  67. }
  68. // Errorf logs an error message.
  69. func Errorf(format string, args ...interface{}) {
  70. lock.Lock()
  71. defer lock.Unlock()
  72. if level < Error {
  73. return
  74. }
  75. msg := fmt.Sprintf("ERROR: %s", format)
  76. if len(args) > 0 {
  77. msg = fmt.Sprintf(msg, args...)
  78. }
  79. log.Println(msg)
  80. }