"Pinchos the son of Elazar the son of Aharon Hakohein turned back my anger from Bnei Yisroel when he zealously avenged Me" (Bamidbar 25, 10). Rashi explains that when Pinchos killed the transgressors, he avenged that which was appropriate for Hashem Himself to avenge.
Rav Wolbe (Shiurei Chumash) notes that Hashem is actually referred to as a "G-d of vengeance" (Devarim 5, 9). What exactly does it mean that Hashem "takes revenge?" He explains that the vengeance of Hashem manifests itself when He eradicates evil from the world. It is therefore an apt description of Hashem, because He wants nothing more than that all evil be eradicated from the world. Pinchos's act of vengeance paralleled Hashem's attribute of vengeance, since it was carried out with a single intention: ridding Bnei Yisroel of the evil. It wasn't a feeling of "sweet revenge" that galvanized Pinchos into action; it was his purity of heart. Hashem doesn't take revenge in the colloquial sense; rather, he eliminates the evil by destroying the transgressors.
It is for this reason that Chazal tell us, "Any talmid chochom who does not take revenge like a snake is not a true talmid chochom." A talmid chochom is one who cleaves to Hashem and the attributes by which He is defined. "O'havei Hashem sin'u rah" - those who love Hashem despise evil, and those who despise evil do everything in their power to eliminate it from society. One who is apathetic to the evil that is being perpetrated around him, cannot be considered a true talmid chochom.
With this approach we can understand why Hashem is meticulous in meting out punishment to those closest to Him, for the slightest infraction. Since the righteous wish to purify themselves, therefore, Hashem aides them by ridding them of even the minutest amount of evil. Likewise, we find that many of our nation's leaders were strictest when it came to disciplining their closest students. When Hagar became pregnant immediately after her marriage to Avraham, she looked down upon Sarah Imeinu who, after many years of marriage, did not merit having children. Subsequently the Torah tells us that Sarah persecuted Hagar. Rav Wolbe explains, because Hagar was a close disciple of Sarah, Sarah felt the need to rid her of all negative traits, and therefore, was very strict with her. Rav Yeruchom Levovitz once grabbed a student and pulled him out of the Beis Medrash because of a misdemeanor. Sometime after, he took that very bochur as a husband for his own daughter!
Both Sarah Imeinu and Rav Yeruchom (and all the great leaders in between) were not "taking revenge" because they felt slighted on a personal level. They were propelled solely by the desire to rid their disciples of their negative character traits. It is this form of "revenge" that is the hallmark of Hashem and those who follow in His ways.