@@ -19,19 +19,20 @@ const (
1919)
2020
2121type EnvComponent struct {
22- ContainerName string `json:"containerName"`
23- ContainerImage string `json:"containerImage"`
24- ContainerVersion string `json:"containerVersion"`
25- ContainerEnvs map [string ]string `json:"containerEnvs"`
26- WasRecreated bool `json:"wasRecreated"`
27- Networks []string `json:"networks"`
28- Container tc.Container `json:"-"`
29- LogStream * logstream.LogStream `json:"-"`
30- PostStartsHooks []tc.ContainerHook `json:"-"`
31- PostStopsHooks []tc.ContainerHook `json:"-"`
32- PreTerminatesHooks []tc.ContainerHook `json:"-"`
33- LogLevel string `json:"-"`
34- StartupTimeout time.Duration `json:"-"`
22+ ContainerName string `json:"containerName"`
23+ ContainerImage string `json:"containerImage"`
24+ ContainerVersion string `json:"containerVersion"`
25+ ContainerEnvs map [string ]string `json:"containerEnvs"`
26+ WasRecreated bool `json:"wasRecreated"`
27+ Networks []string `json:"networks"`
28+ Container tc.Container `json:"-"`
29+ LogStream * logstream.LogStream `json:"-"`
30+ PostStartsHooks []tc.ContainerHook `json:"-"`
31+ PostStopsHooks []tc.ContainerHook `json:"-"`
32+ PreTerminatesHooks []tc.ContainerHook `json:"-"`
33+ LogLevel string `json:"-"`
34+ StartupTimeout time.Duration `json:"-"`
35+ LogConsumerConfig * tc.LogConsumerConfig `json:"-"`
3536}
3637
3738type EnvComponentOption = func (c * EnvComponent )
@@ -94,6 +95,24 @@ func WithPreTerminatesHooks(hooks ...tc.ContainerHook) EnvComponentOption {
9495 }
9596}
9697
98+ func (ec * EnvComponent ) InitLogConsumerConfig (l zerolog.Logger ) error {
99+ if ec .LogStream == nil {
100+ l .Warn ().Msg ("LogStream is nil, cannot initialize LogConsumerConfig. Check your lifecycle and make sure to set LogStream before calling this function" )
101+ return nil
102+ }
103+
104+ logConsumer , err := ec .LogStream .LogConsumerForContainer (ec .ContainerName , "" )
105+ if err != nil {
106+ return err
107+ }
108+
109+ ec .LogConsumerConfig = & tc.LogConsumerConfig {Consumers : []tc.LogConsumer {logConsumer }, Opts : []tc.LogProductionOption {tc .WithLogProductionTimeout (ec .LogStream .GetLogProducerTimeout ())}}
110+
111+ return nil
112+ }
113+
114+ // SetDefaultHooks sets default hooks that connect the container to the log stream and manage log production cycle
115+ // Deprecated: use InitLogConsumerConfig() to use default log production lifecycle
97116func (ec * EnvComponent ) SetDefaultHooks () {
98117 ec .PostStartsHooks = []tc.ContainerHook {
99118 func (ctx context.Context , c tc.Container ) error {
@@ -103,6 +122,7 @@ func (ec *EnvComponent) SetDefaultHooks() {
103122 return nil
104123 },
105124 }
125+ // maybe we could use this step to automatically flush all logs, but we don't know at this point if we should do that (as we don't know if the test has failed)
106126 ec .PostStopsHooks = []tc.ContainerHook {
107127 func (ctx context.Context , c tc.Container ) error {
108128 if ec .LogStream != nil {
0 commit comments