// CreateUser creates a new user resource func CreateUser(context interface{}, db *db.DB, user *User) error { log.Dev(context, "CreateUser", "Started : User: "******"CreateUser", err, "User exists") return "CreateUser: user with same UserName already exists" } */ // set defaults, may want to move this to a factory method on the User struct if user.UserID == "" { user.UserID = uuid.New() } user.MemberSince = time.Now() f := func(col *mgo.Collection) error { log.Dev(context, "CreateUser", "MGO: db.%s.insert()", userCollection) return col.Insert(user) } // Write the user to mongo if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "CreateUser", err, "Completed") return err } log.Dev(context, "CreateUser", "Completed") return nil }
// TestLogLevelDEV tests the basic functioning of the logger in DEV mode. func TestLogLevelDEV(t *testing.T) { t.Log("Given the need to log DEV and USER messages.") { t.Log("\tWhen we set the logging level to DEV.") { log.Init(&logdest, func() int { return log.DEV }) resetLog() defer displayLog() dt := time.Now().Format("2006/01/02 15:04:05") log1 := fmt.Sprintf("%s log_test.go:81: DEV : context : FuncName : Message 1 no format\n", dt) log2 := fmt.Sprintf("%s log_test.go:82: USER : context : FuncName : Message 2 with format: A, B\n", dt) log3 := fmt.Sprintf("%s log_test.go:83: ERROR : context : FuncName : An error : Message 3 with format: C, D\n", dt) log.Dev("context", "FuncName", "Message 1 no format") log.User("context", "FuncName", "Message 2 with format: %s, %s", "A", "B") log.Error("context", "FuncName", errors.New("An error"), "Message 3 with format: %s, %s", "C", "D") if logdest.String() == log1+log2+log3 { t.Logf("\t\t%v : Should log the expected trace line.", succeed) } else { t.Log("***>", logdest.String()) t.Log("***>", log1+log2+log3) t.Errorf("\t\t%v : Should log the expected trace line.", failed) } } } }
// GetUserByID retrieves an individual user by ID func GetUserByID(context interface{}, db *db.DB, id string) (*User, error) { log.Dev(context, "GetUserById", "Started : Id[%s]", id) var user User f := func(c *mgo.Collection) error { q := bson.M{"_id": id} log.Dev(context, "GetUserById", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q)) return c.Find(q).One(&user) } if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "GetUserById", err, "Completed") return nil, err } log.Dev(context, "GetUserById", "Completed") return &user, nil }
// GetUserByUserName retrieves an individual user by email func GetUserByUserName(context interface{}, db *db.DB, userName string) (*User, error) { log.Dev(context, "GetUserByUserName", "Started : User[%s]", userName) userName = strings.ToLower(userName) var user User f := func(c *mgo.Collection) error { q := bson.M{"user_name": userName} log.Dev(context, "GetUserByUserName", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q)) return c.Find(q).One(&user) } if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "GetUserByUserName", err, "Completed") return nil, err } log.Dev(context, "GetUserByUserName", "Completed") return &user, nil }
// CreateComment adds a new comment in the database. func CreateComment(context interface{}, db *db.DB, com *Comment) error { if com.CommentID == "" { com.CommentID = uuid.New() } com.DateCreated = time.Now() com.Status = "New" f := func(col *mgo.Collection) error { log.Dev(context, "CreateComment", "MGO: db.%s.insert()", commentCollection) return col.Insert(com) } if err := db.ExecuteMGO(context, commentCollection, f); err != nil { log.Error(context, "CreateComment", err, "Completed") return err } log.Dev(context, "CreateComment", "Completed") return nil }
// ExampleDev shows how to use the log package. func ExampleDev(t *testing.T) { // Init the log package for stdout. Hardcode the logging level // function to use USER level logging. log.Init(os.Stdout, func() int { return log.USER }) // Write a simple log line with no formatting. log.User("context", "ExampleDev", "This is a simple line with no formatting") // Write a simple log line with formatting. log.User("context", "ExampleDev", "This is a simple line with no formatting %d", 10) // Write a message error for the user. log.Error("context", "ExampleDev", errors.New("A user error"), "testing error") // Write a message error for the user with formatting. log.Error("context", "ExampleDev", errors.New("A user error"), "testing error %s", "value") // Write a message error for the developer only. log.Dev("context", "ExampleDev", "Formatting %v", 42) }