예제 #1
0
파일: log_test.go 프로젝트: xh3b4sd/anna
// Test_Log_005 checks that setting and resetting objects works as expected.
func Test_Log_005(t *testing.T) {
	newRootLogger := testMustNewRootLogger(t)

	newLogConfig := DefaultConfig()
	newLogConfig.RootLogger = newRootLogger
	newLog := New(newLogConfig)

	// Logging a normal log message should work.
	newLog.WithTags(systemspec.Tags{C: nil, L: "W", O: testMustNewObject(t), V: newLogConfig.Verbosity}, "test message")
	result := newRootLogger.(*rootLogger).ArgsToString()
	if !strings.Contains(result, "test message") {
		t.Fatalf("logged message '%s' does not match expected result '%s'", result, "test message")
	}

	newLog.Register(systemspec.ObjectType("strategy-network"))
	newLog.Register(systemspec.ObjectType("impulse"))

	// Reset the test logger to clean the log message of the previous logging.
	newRootLogger.(*rootLogger).ResetArgs()
	// Set objects so log messages should not be logged.
	err := newLog.SetObjects("strategy-network,impulse")
	if err != nil {
		t.Fatalf("Log.SetObjects returned error: %#v", err)
	}

	// Because the object we use is not allowed, the message should not be logged.
	newLog.WithTags(systemspec.Tags{C: nil, L: "W", O: testMustNewObject(t), V: newLogConfig.Verbosity}, "test message")
	result = newRootLogger.(*rootLogger).ArgsToString()
	if result != "" {
		t.Fatalf("logged message '%s' does not match expected result '%s'", result, "")
	}

	// Resetting the objects should log the same log message.
	newRootLogger.(*rootLogger).ResetArgs()
	err = newLog.ResetObjects()
	if err != nil {
		t.Fatalf("Log.ResetObjects returned error: %#v", err)
	}
	newLog.WithTags(systemspec.Tags{C: nil, L: "W", O: testMustNewObject(t), V: newLogConfig.Verbosity}, "test message")
	result = newRootLogger.(*rootLogger).ArgsToString()
	if !strings.Contains(result, "test message") {
		t.Fatalf("logged message '%s' does not match expected result '%s'", result, "test message")
	}
}
예제 #2
0
파일: log.go 프로젝트: xh3b4sd/anna
func (l *log) SetObjects(list string) error {
	l.Mutex.Lock()
	defer l.Mutex.Unlock()

	if list == "" {
		return nil
	}

	newObjects := []spec.ObjectType{}
	for _, objectType := range strings.Split(list, ",") {
		if !containsObjectType(l.RegisteredObjects, spec.ObjectType(objectType)) {
			return maskAnyf(invalidLogObjectError, objectType)
		}

		newObjects = append(newObjects, spec.ObjectType(objectType))
	}

	l.Objects = newObjects
	return nil
}
예제 #3
0
파일: server.go 프로젝트: xh3b4sd/anna
// New creates a new configured server object.
func New(config Config) (spec.Server, error) {
	newServer := &server{
		Config: config,

		BootOnce:   sync.Once{},
		Closer:     make(chan struct{}, 1),
		GRPCServer: grpc.NewServer(),
		HTTPServer: &graceful.Server{
			NoSignalHandling: true,
			Server: &http.Server{
				Addr: config.HTTPAddr,
			},
			Timeout: 3 * time.Second,
		},
		ID:           id.MustNewID(),
		Mutex:        sync.Mutex{},
		ShutdownOnce: sync.Once{},
		Type:         spec.ObjectType(ObjectTypeServer),
	}

	// Dependencies.

	if newServer.Log == nil {
		return nil, maskAnyf(invalidConfigError, "logger must not be empty")
	}
	if newServer.LogControl == nil {
		return nil, maskAnyf(invalidConfigError, "log control must not be empty")
	}
	if newServer.TextInterface == nil {
		return nil, maskAnyf(invalidConfigError, "text interface must not be empty")
	}

	// Settings.

	if newServer.GRPCAddr == "" {
		return nil, maskAnyf(invalidConfigError, "gRPC address must not be empty")
	}
	if newServer.HTTPAddr == "" {
		return nil, maskAnyf(invalidConfigError, "HTTP address must not be empty")
	}

	newServer.Log.Register(newServer.GetType())

	return newServer, nil
}
예제 #4
0
파일: main.go 프로젝트: xh3b4sd/anna
// New creates a new configured command line object.
func New(config Config) (spec.Annactl, error) {
	// annactl
	newAnnactl := &annactl{
		Config: config,

		BootOnce:     sync.Once{},
		Closer:       make(chan struct{}, 1),
		ID:           id.MustNewID(),
		ShutdownOnce: sync.Once{},
		Type:         spec.ObjectType(ObjectType),
	}

	if newAnnactl.Log == nil {
		return nil, maskAnyf(invalidConfigError, "logger must not be empty")
	}
	if newAnnactl.ServiceCollection == nil {
		return nil, maskAnyf(invalidConfigError, "service collection must not be empty")
	}

	return newAnnactl, nil
}
예제 #5
0
파일: log_test.go 프로젝트: xh3b4sd/anna
func (o *object) GetType() systemspec.ObjectType {
	return systemspec.ObjectType("object-type")
}
예제 #6
0
파일: log_test.go 프로젝트: xh3b4sd/anna
// Test_Log_001 checks that different combinations of logger configuration and
// log tags result in logged messages as expected.
func Test_Log_001(t *testing.T) {
	testCases := []struct {
		Tags         systemspec.Tags
		F            string
		V            []interface{}
		Expected     string
		LogContextID string
		LogObjects   []systemspec.ObjectType
		LogLevels    []string
		LogVerbosity int
	}{
		// Logs should not be logged when no tags are given.
		{
			Tags:         systemspec.Tags{},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Logs should not be logged using invalid log level.
		{
			Tags:         systemspec.Tags{C: nil, L: "weird", O: nil, V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "invalid log level: weird",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Logs should be logged with proper formats and operands.
		{
			Tags:         systemspec.Tags{C: nil, L: "I", O: nil, V: 9},
			F:            "test %s %s message",
			V:            []interface{}{"message", "test"},
			Expected:     "test message test message",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Object object logs should be logged when all object logs are allowed by default.
		{
			Tags:         systemspec.Tags{C: nil, L: "I", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "[L: I] [O: object-type / object-id] [V:  9] test message",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Object core logs should not be logged when only object impulse logs are allowed.
		{
			Tags:         systemspec.Tags{C: nil, L: "I", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "",
			LogObjects:   []systemspec.ObjectType{systemspec.ObjectType("impulse")},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Verbosity 9 logs should not be logged when only verbosity 8 logs and lower are allowed.
		{
			Tags:         systemspec.Tags{C: nil, L: "I", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 8,
		},

		// Debug logs should not be logged when only info logs are allowed.
		{
			Tags:         systemspec.Tags{C: nil, L: "D", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{"I"},
			LogVerbosity: 10,
		},

		// Debug logs should be logged when only debug logs are allowed.
		{
			Tags:         systemspec.Tags{C: nil, L: "D", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "test message",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{"D"},
			LogVerbosity: 10,
		},

		// Log message using trace ID should be logged when tracer is given in log
		// tags and log config's trace ID is empty by default.
		{
			Tags:         systemspec.Tags{C: testMustNewContext(t), L: "D", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "[C: context-id] [L: D] [O: object-type / object-id] [V:  9] test message",
			LogContextID: "",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},

		// Log message using trace ID should NOT be logged when tracer is given in
		// log tags and log config's trace ID is different.
		{
			Tags:         systemspec.Tags{C: testMustNewContext(t), L: "D", O: testMustNewObject(t), V: 9},
			F:            "test message",
			V:            []interface{}{},
			Expected:     "",
			LogContextID: "other",
			LogObjects:   []systemspec.ObjectType{},
			LogLevels:    []string{},
			LogVerbosity: 10,
		},
	}

	for i, testCase := range testCases {
		newRootLogger := testMustNewRootLogger(t)

		newLogConfig := DefaultConfig()
		newLogConfig.ContextID = testCase.LogContextID
		newLogConfig.Objects = testCase.LogObjects
		newLogConfig.Levels = testCase.LogLevels
		newLogConfig.RootLogger = newRootLogger
		newLogConfig.Verbosity = testCase.LogVerbosity
		newLog := New(newLogConfig)

		newLog.WithTags(testCase.Tags, testCase.F, testCase.V...)

		result := newRootLogger.(*rootLogger).ArgsToString()

		if (testCase.Expected == "" && result != "") || (!strings.Contains(result, testCase.Expected)) {
			t.Fatalf("%d. test case failed: logged message '%s' does not match expected result '%s'", i+1, result, testCase.Expected)
		}
	}
}