CITE Sefer Chofetz Chaim chapter 10 halacha two concluded

Rule #6: Before speaking out against a perpetrator you have to be sure there is no other way to fix the issue besides speaking lashon hara about him whether it is to help the victim or to stop this type of behavior, for example if you can say the story of what happened without any name calling to send the message that what happened should not be done that would be better. The extent of avoiding lashon hara we see from the case of Achan in the Book of Yehoshua. Achan stole spoils of war secretly. Yehoshua wanted to know what happened. He asked Hashem who did it. Hashem said what do you think I am, a tattletaler? I won’t speak lashon hara, make a lottery and the one picked is the one guilty. Hashem would not straight out tell Yehoshua who the culprit was rather it was indirectly Divinely revealed through a lottery because another means of revealing with saying out right was possible. Hashem acted through these means even though the gemara in Sanhedrin 11a and 44a says that not only did Achan steal the spoils that were sanctified but he also was adulterous to the highest degree with a naara hami’urasa, it was because of him that the Jews last a battle, and he transgressed the Five Books of Moses. This should put him into a category of not being part of “your nation” and therefore it should have been permissible to speak lashon hara about him. However, because there was another way to reveal his guilt it was forbidden for Hashem to tell Yehoshua that it was Achan who is causing all this trouble. We find this concept of needing to act differently, if possible, by other halachos as well. For example, if someone is running after another to kill him and you can stop it by disability the pursuer without killing him, then if you kill the pursuer, you are considered a murderer. So to if someone is going to hit someone else and you can defend the victim or the victim can defend himself with hurting the attacker then one would be guilty for hurting the attacker. When the Shulchan Aruch Choshen Mishpat 421:13 says the victim or someone around him can hit back a person who struck him the Shach says that’s only if he is defending himself so that the attacker will not strike again.

Rule #7: If by you telling others you will harm the perpetrator more than he would deserve if he was taken to court then you can’t tell anybody. Not only does this mean that if two of you tell others, let say about a thief, so you are like two witnesses but those that find out will not only go after the thief to force him to return the goods, but they will also beat him up, then you can’t tell them even at the expense of the victim never getting his stolen goods back. If you are only one person divulging the information to others, then the best you can do is ensure that those people could convince the perpetrator to show up in court and let the court deal with the problem just like a single witness can only force the perpetrator to swear in court but can’t be the reason for the sentencing.

If all 7 rules are met and still the problem isn’t resolved or the message isn’t clear for others to stay away from doing these actions, or the perpetrator has not repented then others can be told with divulging names so that the problem can be more easily resolved.