logs.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package utils
  14. import (
  15. "github.com/docker/compose-cli/pkg/api"
  16. )
  17. // FilteredLogConsumer filters logs for given services
  18. func FilteredLogConsumer(consumer api.LogConsumer, services []string) api.LogConsumer {
  19. if len(services) == 0 {
  20. return consumer
  21. }
  22. allowed := map[string]bool{}
  23. for _, s := range services {
  24. allowed[s] = true
  25. }
  26. return &allowListLogConsumer{
  27. allowList: allowed,
  28. delegate: consumer,
  29. }
  30. }
  31. type allowListLogConsumer struct {
  32. allowList map[string]bool
  33. delegate api.LogConsumer
  34. }
  35. func (a *allowListLogConsumer) Log(container, service, message string) {
  36. if a.allowList[service] {
  37. a.delegate.Log(container, service, message)
  38. }
  39. }
  40. func (a *allowListLogConsumer) Status(container, message string) {
  41. if a.allowList[container] {
  42. a.delegate.Status(container, message)
  43. }
  44. }
  45. func (a *allowListLogConsumer) Register(name string) {
  46. if a.allowList[name] {
  47. a.delegate.Register(name)
  48. }
  49. }